1

How I can send a custom response and message on status 400+, 500+?

export const (req, res) { 
// external api call
const response = await axios.post(...)
//handle response 
res.status(200).json(response.data)
// if has an error
res.status(400).json(response.data) } 

On front-end, I don't receive the payload data if not status 200

try { 
  let response = await.post(...) 
  console.log(response.data) 
} catch (error) { 
  console.log(error) // i cant get the payload passed here if not status 200 
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • Did you manage to get the error payload? In my case I forgot the "await" for `res.json()` – Steffen Harbich Mar 31 '21 at 15:11
  • 2
    Does this answer your question: [catching error body using axios post](https://stackoverflow.com/questions/45017822/catching-error-body-using-axios-post)? You can retrieve the response payload through `error.response.data`. – juliomalves Oct 03 '21 at 15:37

0 Answers0