0

I'm working with Laravel 5.6 as my backend for a personal project and i've been doing something that seems (to me) as a bad practice, either way, I would like to know if it is actually that bad.

First of all, i'm using a Vue.js (CLI 3) project as a client and i'm making requests to my Laravel backend. Now, to deal with the notifications/toasts, i'm using the next format:

return response()->json([
  'alert' => [
      'title' => 'Server error.',
      'text' => 'Error explanation text.',
      'type' => 'error'
  ]
], 200);

It doesn't matter if I everything went right or wrong, i am always responding with this same format and an 200 status. Is it wrong? Should I use other statuses on my responses?

I am doing this because i can't get (i don't know how) the custom 'alert' array on my client side while using a 404 status (for example) and the only way I could find to deal with it was using this 200 status every single time.

enbermudas
  • 1,603
  • 4
  • 20
  • 42

1 Answers1

2

HTTP status code are a mechanism to identify easily a response and will help to understand to clients if a requests was okey just checking it, for example a search engine robot that could distinguish between a error page just thanks to the status code.

In Axios for example, a HTTP client for JS, you can read response data even if was error https://stackoverflow.com/a/39153411.

Also have a look into this resource that will help you which status code to choose https://www.restapitutorial.com/httpstatuscodes.html

jesugmz
  • 2,320
  • 2
  • 18
  • 33
  • I took a look to the `axios` library it actually lets me work with the error response like this: `error.response.data.alert` so it woill work just find. Thanks for your answer! – enbermudas Aug 11 '18 at 13:53