0

At times I return, at server level, some extra information alongside with a HTTP 404

For example, instead of returning just a 404, which can puzzle my client whether the routing is correct or not, it will also receive something like

the identifier 'abc' is unknown

I usually set the content type to text/plain and return some text in the Content

Another alternative is to set the ReasonPhrase instead.

Which one is the best way / convention? Set Content or set ReasonPhrase?

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76

2 Answers2

2

The error message should be put in response body (Content), not in response Reason Phrase.

According to RFC 2616

The Reason-Phrase is intended to give a short textual description of the Status-Code...The client is not required to examine or display the Reason-Phrase.

Some explanation:

  1. Reason-Phrase is short, textual description of Status-Code, it should describe Status Code itself, not custom error message. If custom error message is very long, or the message has JSON structure, using Reason-Phrase certainly violates the specification.
  2. As the specification indicate, the client (browser) is not required to examine the Reason-Phrase, which means Reason-Phrase may get ignored for some browsers, in some time.
shaochuancs
  • 15,342
  • 3
  • 54
  • 62
0

You can use custom error responses and overrides the 404 and any other error you want visit here Spring MVC: How to return custom 404 errorpages?

Create a view and set this code in app/Exception/Handler.php :

/*Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response

*/

public function render($request, Exception $e)

{

if($e instanceof NotFoundHttpException)

{

    return response()->view('missing', [], 404);

}

return parent::render($request, $e);

}

Set this use to get it working :

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

For more info you can visit

https://httpd.apache.org/docs/2.4/custom-error.html

https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs

Usama Jamil
  • 68
  • 10
  • My question is not so much on how we can do it but if it is actually a good idea doing it. – Luis Filipe Jun 26 '17 at 12:00
  • No its not a good idea, Because if the client is performing any specific action on 404 have to change their code make it compatible with your code or they might don't know how to do it. Sometimes its good to follow standards..... I would no recommend that. – Usama Jamil Jun 26 '17 at 16:47