Search code examples
javascriptreact-nativepostmanfetch-api

Reading error message returned by postman in react native app


I have an api used for login in my react native app. The api is implemented and working as its supposed to. But the issue is that if the user enters wrong email or password m not receiving the error message in the response in the application. While if i test the api manually in postman it returns the error message required. i tried making a transformer for the error response but m not knowing how to implement it or use it. I'm using fetch to call my apis.

return fetch(fullUrl, requestParameters)
    .then((response) => {
      if(response.ok) {
        return response.headers.get("content-type") === "application/json" ? response.json() : null
      } else {
        errorrr = ErrorTransformer.backward(response)
        console.log("Error: ", errorrr)
}

And below is the tranformer made for the error response

import {createTransformer} from './Transformer';


const ErrorTransform ={
  o:[
    ['message','message'],
    ['code','code'],
  ]
}

export default createTransformer(ErrorTransform)

And below is the response returned from postman when wrong info are entered

{
    "message": "error",
    "code": 1
}

Solution

  • After a lot of trials, i figured that the postman response when the user enters wrong email or password is a regular response object, it’s not an error. So its handling should be like a regular response. But what remained as an obstacle is to tell apart when i have a success response with the info returned, or when i have the error object returned. I solved this issue by combining the error transformer with the login transformer, and its handling it perfectly.