22 lines
636 B
Python
22 lines
636 B
Python
|
|
"""Uniform JSON error handlers."""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from flask import Flask, jsonify
|
||
|
|
from werkzeug.exceptions import HTTPException
|
||
|
|
|
||
|
|
|
||
|
|
def register_error_handlers(app: Flask) -> None:
|
||
|
|
@app.errorhandler(HTTPException)
|
||
|
|
def handle_http_exception(exc: HTTPException):
|
||
|
|
response = jsonify({"error": exc.description})
|
||
|
|
response.status_code = exc.code or 500
|
||
|
|
return response
|
||
|
|
|
||
|
|
@app.errorhandler(404)
|
||
|
|
def handle_404(_exc):
|
||
|
|
return jsonify({"error": "Not found"}), 404
|
||
|
|
|
||
|
|
@app.errorhandler(405)
|
||
|
|
def handle_405(_exc):
|
||
|
|
return jsonify({"error": "Method not allowed"}), 405
|