I am using async in nodeJS, and in my final callback I am handling the error and trying to send it back to my angular controller.
function (err, data) {
if (err) {
console.log(err);
res.status(500).send({ err : err});
}
else {
res.json({data: data});
}
});
Now the error in the console is.
[Error: Username is already in use]
I am not able to get this particular error in my angular controller I tried sending the error in all binations such as .
res.status(500).send({ err : err[0]});
res.status(500).send({ err : err.Error});
This is what I get in my front end.
Object {data: Object, status: 500, config: Object, statusText: "Internal Server Error"}
config
:
Object
data
:
Object
err
:
Object
__proto__
:
Object
__proto__
:
Object
headers
:
(d)
status
:
500
statusText
:
"Internal Server Error"
How can I bring that username in use error to my front End.
I am using async in nodeJS, and in my final callback I am handling the error and trying to send it back to my angular controller.
function (err, data) {
if (err) {
console.log(err);
res.status(500).send({ err : err});
}
else {
res.json({data: data});
}
});
Now the error in the console is.
[Error: Username is already in use]
I am not able to get this particular error in my angular controller I tried sending the error in all binations such as .
res.status(500).send({ err : err[0]});
res.status(500).send({ err : err.Error});
This is what I get in my front end.
Object {data: Object, status: 500, config: Object, statusText: "Internal Server Error"}
config
:
Object
data
:
Object
err
:
Object
__proto__
:
Object
__proto__
:
Object
headers
:
(d)
status
:
500
statusText
:
"Internal Server Error"
How can I bring that username in use error to my front End.
Share Improve this question asked Oct 6, 2016 at 6:27 sac Dahalsac Dahal 1,2412 gold badges16 silver badges37 bronze badges 8- What does your function do? – abdulbari Commented Oct 6, 2016 at 6:29
- res.status(500).send({ err : err});, here I am trying to send the err, where err is [Error: Username is already in use], but its not goingto front-end. – sac Dahal Commented Oct 6, 2016 at 6:31
- why not return a ok status with the error 500 is for server errors not data validation errors – madalinivascu Commented Oct 6, 2016 at 6:32
- but I want [Error: Username is already in use] to show to users – sac Dahal Commented Oct 6, 2016 at 6:32
-
what is the value of
{data: Object
in front-end ? – abdulbari Commented Oct 6, 2016 at 6:33
1 Answer
Reset to default 6500
errors are usually reserved for Server errors, and not for scenarios like the one you have described. Server errors should be handled by your server and elegantly presented to your front end. Client errors should be in the 400s. Why don't you try a 409
or a 400
:
res.status(409).json({error: "Username is already taken"});
Look at HTTP Status codes for more:
409 Conflict The request could not be pleted due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.
Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't plete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.
Note: Also, as a good practice, make sure you return your res.
functions, for predictable flow of control, like so:
return res.status(409).json({error: "Username is already taken"});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744938711a4602186.html
评论列表(0条)