Handling REST URL Errors Using the Play Platform

Currently, when I (or, more importantly, a user) in one of my leisure functions in a URL, it works with a status code of 200. But if you enter the wrong code or use it incorrectly, a 404 page is created with the status code 404 when viewed through a REST client.

Instead of sending 404 pages when sending a bad URL, I would like to display a dynamically generated JSON object instead.

How to fix this error handling to do what I want, is there a place where I can determine what should be done during a specific status code?

+6
source share
1 answer

I am not very familiar with the Play Framework, but I was interested. This discussion seemed at least similar to what you want :

Gaetan Renaudo

...

You can customize error pages depending on the error of the http code (404, 500, 403, ...) by editing app / views / errors / {code} .html files where {code} is your http code. If you use a different format than html (e.g. xml, json), you can 404.json, 404.xml, etc.

So change:

app/views/errors/404.type_of_response 

Hopefully this at least indicates that you're in the right direction.

+7
source

Source: https://habr.com/ru/post/889995/


All Articles