Skip to content

Commit

Permalink
Merge pull request #127 from lalithkota/15.0-develop
Browse files Browse the repository at this point in the history
Rest Api: Fixed exception handling for all exception types
  • Loading branch information
shibu-narayanan authored Mar 1, 2024
2 parents 32b5cb5 + d478f69 commit 59e0f24
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
17 changes: 2 additions & 15 deletions g2p_registry_rest_api/http.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from werkzeug.exceptions import BadRequest, HTTPException, InternalServerError
from werkzeug.exceptions import BadRequest, InternalServerError

import odoo
from odoo.http import Root
Expand All @@ -14,8 +14,6 @@


def g2pFixException(exception, original_exception=None):
get_original_headers = HTTPException(exception).get_headers

def get_body(environ=None, scope=None):
if original_exception and isinstance(original_exception, G2PApiException):
res = G2PErrorResponse(
Expand All @@ -36,15 +34,6 @@ def get_body(environ=None, scope=None):
res.update({"traceback": exception.traceback})
return JSONEncoder().encode(res)

def get_headers(environ=None, scope=None):
"""Get a list of headers."""
_headers = [("Content-Type", "application/json")]
for key, value in get_original_headers(environ=environ, scope=scope):
if key != "Content-Type":
_headers.append(key, value)
return _headers

exception.get_headers = get_headers
exception.get_body = get_body
return exception

Expand All @@ -54,11 +43,9 @@ def _handle_exception(self, exception):
res = super()._handle_exception(exception)
if isinstance(exception, G2PApiValidationError):
res = wrapJsonException(BadRequest(exception.args[0]))
g2pFixException(res, exception)
elif isinstance(exception, G2PApiException):
res = wrapJsonException(InternalServerError(exception.args[0]))
g2pFixException(res, exception)

g2pFixException(res, exception)
return res


Expand Down
6 changes: 3 additions & 3 deletions g2p_registry_rest_api/models/registrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def validate_id_type_no_spaces(cls, value): # noqa: B902
return value

@validator("value")
def validate_id_value(cls, value, values):
def validate_id_value(cls, value, values): # noqa: B902
id_type = values.get("id_type")
if id_type:
id_type_id = request.env["g2p.id.type"].search(
Expand Down Expand Up @@ -121,7 +121,7 @@ def validate_email(cls, value): # noqa: B902
return value

@validator("registration_date")
def validate_registration_date(cls, value):
def validate_registration_date(cls, value): # noqa: B902
if value is not None and value > date.today():
raise G2PApiValidationError(
error_message=G2PErrorCodes.G2P_REQ_011.get_error_message(),
Expand All @@ -131,7 +131,7 @@ def validate_registration_date(cls, value):
return value

@validator("name")
def validate_name_presence(cls, value):
def validate_name_presence(cls, value): # noqa: B902
if value is None or value.strip() == "":
raise G2PApiValidationError(
error_message=G2PErrorCodes.G2P_REQ_012.get_error_message(),
Expand Down

0 comments on commit 59e0f24

Please sign in to comment.