Reputation:
i want make SPA application with vue and laravel . for return errors , i have 2 ways ... what is best choice?
1:
return response(['message'=>'page not found'] , 404);
2:
return response(['status'=>false , 'code'=>404 ,'message'=>'page not found'] , 200);
I think the second way is better. Because it does not show the error in the browser console .. What is your comment?
Upvotes: 0
Views: 377
Reputation: 6788
This question is really broad, since I think you're not asking about specific Vue/Laravel details, but rather considering how to treat errors as a whole.
I don't know about specific Laravel stuff, but it seems that you want to return HTTP request that fire errors as if they were correct, but then setup a custom code
field so that your front-end application knows they are actually errors.
So an user ask for a resource in the server, and if the resource does not exist, you return a 200 but with a 404 code.
This is some sort of messy and non-standard, and I would suggest to return realistic HTTP codes. There are three main reasons for this:
There are some services that profile application network traffic and generate dedicated reports. They rely on standard HTTP response codes. IF in the future you want to use such services, it is better you don't fake the HTTP codes from your responses.
New developers that eventually will start to work on your project will be puzzled by this.
Also there are third-party services that catch front-end errors, network responses and traces and generate statistics. Again, if you get a 404, or a 503 or any HTTP error, it may be useful to get it logged, and to get the correct HTTP status code, due to this.
Upvotes: 1