fix flask_restx error handler, update README.md, give up on fixing require_auth() on flask_restx

This commit is contained in:
Yusur 2025-07-01 14:48:58 +02:00
parent 96d25e0e85
commit f0a109983b
6 changed files with 19 additions and 9 deletions

View file

@ -16,10 +16,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from typing import Any, Mapping
import warnings
from flask import current_app, make_response
from flask import current_app, Response, make_response
from flask_restx import Api as _Api
from .codecs import jsonencode
from .codecs import jsondecode, jsonencode, want_bytes, want_str
def output_json(data, code, headers=None):
@ -54,13 +54,21 @@ class Api(_Api):
Notably, all JSON is whitespace-free and .message is remapped to .error
"""
def handle_error(self, e):
### XXX apparently this handle_error does not get called AT ALL.
print(e)
### XXX in order for errors to get handled the correct way, import
### suou.flask_restx.Api() NOT flask_restx.Api() !!!!
res = super().handle_error(e)
print(res)
if isinstance(res, Mapping) and 'message' in res:
res['error'] = res['message']
del res['message']
elif isinstance(res, Response):
try:
body = want_str(res.response[0])
bodj = jsondecode(body)
if 'message' in bodj:
bodj['error'] = bodj.pop('message')
res.response = [want_bytes(jsonencode(bodj))]
except (IndexError, KeyError):
pass
return res
def __init__(self, *a, **ka):
super().__init__(*a, **ka)