Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft : Adding provision to pass inputs to custom error handler class #919

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rohan-97
Copy link

Adding an optional parameter error_handler_params in Flask Decorator to provide data to Error handler class.

The parameter is basically a dictionary which is passed to constructor of error handler class,
and if it is not passed, then nothing is passed in the constructor.

This PR implements following feature request
#910

Consider below example for understanding usage of this flag

#!/usr/bin/python3
"""Test server."""

from flask import Flask, request, jsonify
from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator, FlaskOpenAPIErrorsHandler
from openapi_core import Spec

# Custom Error Handler block
class MyCustomErrorHandler(FlaskOpenAPIErrorsHandler):
    """"Custom Error Handler"""
    def __init__(self, input_param:dict):
        self.title =  input_param.get("title")

    def handle(self, errors:list):
        response_object =  jsonify({
            "title": self.title,
            "causedBy" : [self.handle_error(error) for error in errors]
        })
        response_object.error_code = 400
        return response_object

    def handle_error(self, error):
        """
        Converts error object into error string message

        :param error: Error object which stores exception message
        :type error: Exception object
        :return: Error message string corresponding to error object
        :rtype: str
        """
        if error.__cause__ is not None:
            error = error.__cause__
        return str(error)

SPEC = "test.yaml"
app = Flask(__name__)

spec_object = Spec.from_file_path(SPEC)
openapi_decorator = FlaskOpenAPIViewDecorator.from_spec(spec_object, openapi_errors_handler=MyCustomErrorHandler, error_handler_params={"title" : "Failed to execute test API"})

@app.route("/test", methods=["POST"])
@openapi_decorator
def read_permission():
    """Test function"""
    temp =  jsonify({
                    "stri_normal_json": request.json.get("flag", 1)
                })
    print(dir(temp))
    return temp

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=345, debug=True)

#Valid request: curl -X POST http://localhost:345/test --data '{"flag":"rohana"}' -H 'Content-Type: application/json'

#Invalid Request: curl -X POST http://localhost:345/test --data '{"flag":"rohan"}' -H 'Content-Type: application/json'

Following is test.yaml

openapi: '3.0.2'
info:
  title: Test Title
  version: '1.0'
servers:
  - url: http://localhost:345/
paths:
  /test:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required:
                - flag
              properties:
                flag:
                  x-pii: true
                  type: string
                  pattern: "^[\\w.-]*$"
                  minLength: 6
                  maxLength: 20
      responses:
        200:
          description: Sample response
          content:
            application/json:
              schema:
                type: object
                properties:
                  stri_normal_json:
                    type: string
                    minLength: 6
                    maxLength: 20

Kindly note the constructor of openapi error handler

def __init__(self, input_param:dict):

whatever custom input is passed is accessed through input_param parameter.
int input_param is a dictionary and stores custom data in custom key/value format.

this custom input can be passed whenever we create new openapi_core decorator object as shown in above example

FlaskOpenAPIViewDecorator.from_spec(spec_object, openapi_errors_handler=MyCustomErrorHandler, error_handler_params={"title" : "Failed to execute test API"})

as we can see the dictionary {"title" : "Failed to execute test API"} is passed to error handler constructor and is accessed within the code

@rohan-97
Copy link
Author

As there were no docstrings present, the new parameter is not documented
do let me know if it needs to be documented somewhere

@rohan-97 rohan-97 changed the title Adding provision to pass inputs to custom error handler class Draft : Adding provision to pass inputs to custom error handler class Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant