| 45 | |
| 46 | |
| 47 | def register_errors(blueprint: Blueprint): # noqa: C901 |
| 48 | @blueprint.errorhandler(InvalidRecipientError) |
| 49 | def invalid_format(error: InvalidRecipientError) -> ResponseReturnValue: |
| 50 | return jsonify(result="error", message=str(error)), 400 |
| 51 | |
| 52 | @blueprint.errorhandler(AuthError) |
| 53 | def authentication_error(error: AuthError) -> ResponseReturnValue: |
| 54 | return jsonify(result="error", message=error.message), error.code |
| 55 | |
| 56 | @blueprint.errorhandler(ValidationError) |
| 57 | def marshmallow_validation_error(error: ValidationError) -> ResponseReturnValue: |
| 58 | current_app.logger.info(error) |
| 59 | return jsonify(result="error", message=error.messages), 400 |
| 60 | |
| 61 | @blueprint.errorhandler(JsonSchemaValidationError) |
| 62 | def jsonschema_validation_error(error: JsonSchemaValidationError) -> ResponseReturnValue: |
| 63 | current_app.logger.info(error) |
| 64 | return jsonify(json.loads(error.message)), 400 |
| 65 | |
| 66 | @blueprint.errorhandler(ArchiveValidationError) |
| 67 | def archive_validation_error(error: ArchiveValidationError) -> ResponseReturnValue: |
| 68 | current_app.logger.info(error) |
| 69 | return jsonify(result="error", message=str(error)), 400 |
| 70 | |
| 71 | @blueprint.errorhandler(InvalidRequest) |
| 72 | def invalid_data(error: InvalidRequest) -> ResponseReturnValue: |
| 73 | response = jsonify(error.to_dict()) |
| 74 | response.status_code = error.status_code |
| 75 | current_app.logger.info(error) |
| 76 | return response |
| 77 | |
| 78 | @blueprint.errorhandler(400) |
| 79 | def bad_request(e: HTTPException) -> ResponseReturnValue: |
| 80 | msg = e.description or "Invalid request parameters" |
| 81 | current_app.logger.exception(msg) |
| 82 | return jsonify(result="error", message=str(msg)), 400 |
| 83 | |
| 84 | @blueprint.errorhandler(401) |
| 85 | def unauthorized(e: HTTPException) -> ResponseReturnValue: |
| 86 | error_message = "Unauthorized: authentication token must be provided" |
| 87 | return jsonify(result="error", message=error_message), 401, [("WWW-Authenticate", "Bearer")] |
| 88 | |
| 89 | @blueprint.errorhandler(403) |
| 90 | def forbidden(e: HTTPException) -> ResponseReturnValue: |
| 91 | error_message = "Forbidden: invalid authentication token provided" |
| 92 | return jsonify(result="error", message=error_message), 403 |
| 93 | |
| 94 | @blueprint.errorhandler(429) |
| 95 | def limit_exceeded(e: HTTPException) -> ResponseReturnValue: |
| 96 | current_app.logger.exception(e) |
| 97 | return jsonify(result="error", message=str(e.description)), 429 |
| 98 | |
| 99 | @blueprint.errorhandler(NoResultFound) |
| 100 | @blueprint.errorhandler(DataError) |
| 101 | def no_result_found(e: NoResultFound | DataError) -> ResponseReturnValue: |
| 102 | current_app.logger.info(e, exc_info=True) |
| 103 | return jsonify(result="error", message="No result found"), 404 |
| 104 | |