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?

8
  • Possible duplicate of Proper way to set response status and JSON content in a REST API made with nodejs and express Commented Aug 15, 2018 at 18:28
  • i looked at all these answers. I need something else. Commented Aug 15, 2018 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 Commented Aug 15, 2018 at 19:15
  • why is it text? I know the API res (when i try in postman) returns json... turns it to text somewhere Commented Aug 15, 2018 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. Commented Aug 15, 2018 at 19:25

2 Answers 2

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)
});
Sign up to request clarification or add additional context in comments.

3 Comments

That gives me json but not the full message... see attempt 5 (just added)
You're still doing it wrong. I'll update my answer to be more clear.
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
-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();
    }
});

3 Comments

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 can you explain it a bit more about the context like what request you are sending etc, this will help us find the problem.
Did you tried sending the request via other platform or any browser??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.