40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from flask import json, make_response
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
from .api import api
|
|
from ..controller.exceptions import LibraryExceptionBase
|
|
|
|
@api.errorhandler(LibraryExceptionBase)
|
|
def handle_exception(e):
|
|
"""Return JSON instead of HTML for HTTP errors."""
|
|
# start with the correct headers and status code from the error
|
|
response = make_response()
|
|
# replace the body with JSON
|
|
response.data = json.dumps({
|
|
"status": "error",
|
|
"code": e.code,
|
|
"status_code": e.status_code,
|
|
"name": e.name,
|
|
"error": e.error,
|
|
})
|
|
response.content_type = "application/json"
|
|
response.status_code = e.status_code
|
|
return response
|
|
|
|
@api.errorhandler(HTTPException)
|
|
def handle_exception(e):
|
|
"""Return JSON instead of HTML for HTTP errors."""
|
|
# start with the correct headers and status code from the error
|
|
response = e.get_response()
|
|
# replace the body with JSON
|
|
response.data = json.dumps({
|
|
"status": "error",
|
|
"code": f"000{e.code}",
|
|
"status_code": e.code,
|
|
"name": e.name,
|
|
"error": e.description,
|
|
})
|
|
response.content_type = "application/json"
|
|
return response
|
|
|