Skip to content

atemon/CustomExceptions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Exceptions

For APIs, we may need to have custom exceptions raised from server. To keep exception codes and messages consistant across projects will make development faster and easier.

Here is a sample work which could be used as is or canbe used as a model for defining exceptions.

Usage

from exceptions.api import InputValidationError

Convert standard exceptions

if (request.method == "PUT" or request.method == "POST" or request.method == "PATCH") and request.content_type == "application/json":
    try:
        request.JSON = json.loads(request.body)
    except ValueError as ve:
        return JsonResponse(InputValidationError(exception=ve))
else:
    request.JSON = {}

Custom Exception

try:
    self.offset = int(request.GET.get("offset", '0'))
except Exception:
    raise InputValidationError(message_dict={"offset": "Offset should be an integer(convertible). Received '{0}'".format(request.GET.get("offset"))})

Serialize to JSON

message = exception.jsonify()

Serialize to dict

message = exception.dict()

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages