0

I am trying to write a middleware to handle errors. But I cannot figure out how to send the correct format to my frontend. Below I am going to list all of my attempts in hopes of helping you help me.

Attempt 1

app.use(function(err, req, res, next) {
    const formatted = err;
    res.send(formatted)
});

result in postman

{ "code": 422 }

Attempt 2

app.use(function(err, req, res, next) {
    const formatted = `${err}`;
    res.send(formatted)
});

result (postman)

Error: Request returned error code: 422 and body: {"status":422,"title":"The display name: build_id has already been used on this product.","type":"https://developer.bigcommerce.com/api#api-status-codes","errors":{"display_name":"The display name: build_id has already been used on this product."}}

That is the data i want but i need it in json

Question Why is there more data revealed after string interpolation? How can i format it to json?

Omar
  • 2,726
  • 2
  • 32
  • 65
  • Possible duplicate of [Proper way to set response status and JSON content in a REST API made with nodejs and express](https://stackoverflow.com/questions/26066785/proper-way-to-set-response-status-and-json-content-in-a-rest-api-made-with-nodej) – Robert Moskal Aug 15 '18 at 18:28
  • i looked at all these answers. I need something else. – Omar Aug 15 '18 at 18:55
  • The error your `console.log()` is not JSON it is a text. I think your error is related to **bigcommerce** as it returned in the message – Hello World Aug 15 '18 at 19:15
  • why is it text? I know the API res (when i try in postman) returns json... turns it to text somewhere – Omar Aug 15 '18 at 19:17
  • `Error: Request returned error code: 422 and body: {"status":422,"title":"The display name: build_id has already been used on this product.","type":"https://developer.bigcommerce.com/api#api-status-codes","errors":{"display_name":"The display name: build_id has already been used on this product."}}` <-- This is actually a text/string. I don't know why !! On my understanding this is related to any of your external library which is causing it. I would suggest you to focus on the problem you are getting in `console` which is `The display name: build_id has already been used on this product.` – Hello World Aug 15 '18 at 19:25
  • ... and Then focus on the middleware issue. The response `{ "code": 422 }` is all we have. Problem is not in the middleware. – Hello World Aug 15 '18 at 19:28
  • i dont want to fix the error. I want to send the error to my frontend – Omar Aug 15 '18 at 19:30
  • I have edited my answer please have a look. – Hello World Aug 15 '18 at 19:35

2 Answers2

0

You're probably looking for res.json(err) if I had to guess?

https://expressjs.com/en/api.html#res.json

EDIT: Full example:

app.use(function(err, req, res, next) {
    if (err) res.json(err)
});
rickjerrity
  • 804
  • 1
  • 9
  • 15
  • That gives me json but not the full message... see `attempt 5` (just added) – Omar Aug 15 '18 at 18:30
  • You're still doing it wrong. I'll update my answer to be more clear. – rickjerrity Aug 15 '18 at 18:33
  • That is not retuning the full message. `>{"code": 422 }`... Look at the difference between attempt one and two. That is the problem i need to figure out – Omar Aug 15 '18 at 18:38
-1

You can do it in this way

app.use(function(err, req, res, next) {
    if (err) {
        res.json({
            status: "error",
            error: `${err}` // <-- edited
        });
    } else {
        next();
    }
});
Hello World
  • 2,673
  • 7
  • 28
  • 60
  • If you look at attempt 1 there is only `{ "code": 422 }` until attempt 2 where i did string interpolation. That is the same problem im having when i tried this. Only the code returns but not the message. – Omar Aug 15 '18 at 18:36
  • @Omar can you explain it a bit more about the context like what request you are sending etc, this will help us find the problem. – Hello World Aug 15 '18 at 18:40
  • Did you tried sending the request via other platform or any browser?? – Hello World Aug 15 '18 at 18:42