Skip to content

Commit

Permalink
Create models for all 'possible' errors for use by OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs authored and CasperWA committed Jun 10, 2021
1 parent 2411ad1 commit 9054d09
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 15 deletions.
95 changes: 83 additions & 12 deletions optimade/server/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"BadRequest",
"VersionNotSupported",
"Forbidden",
"NotFound",
"UnprocessableEntity",
"NotImplementedResponse",
"POSSIBLE_ERRORS",
)


Expand All @@ -20,40 +24,107 @@ def __str__(self):
class BadRequest(StrReprMixin, HTTPException):
"""400 Bad Request"""

status_code: int = 400
title: str = "Bad Request"

def __init__(
self,
status_code: int = 400,
detail: str = None,
headers: dict = None,
title: str = "Bad Request",
) -> None:
super().__init__(status_code=status_code, detail=detail, headers=headers)
self.title = title
super().__init__(status_code=self.status_code, detail=detail, headers=headers)


class VersionNotSupported(StrReprMixin, HTTPException):
"""553 Version Not Supported"""

status_code: int = 553
title: str = "Version Not Supported"

def __init__(
self,
status_code: int = 553,
detail: str = None,
headers: dict = None,
title: str = "Version Not Supported",
) -> None:
super().__init__(status_code=status_code, detail=detail, headers=headers)
self.title = title
super().__init__(status_code=self.status_code, detail=detail, headers=headers)


class Forbidden(StrReprMixin, HTTPException):
"""403 Forbidden"""

status_code: int = 403
title: str = "Forbidden"

def __init__(
self,
detail: str = None,
headers: dict = None,
) -> None:
super().__init__(status_code=self.status_code, detail=detail, headers=headers)


class NotFound(StrReprMixin, HTTPException):
"""404 Not Found"""

status_code: int = 404
title: str = "Not Found"

def __init__(
self,
detail: str = None,
headers: dict = None,
) -> None:
super().__init__(status_code=self.status_code, detail=detail, headers=headers)


class UnprocessableEntity(StrReprMixin, HTTPException):
"""422 Unprocessable Entity"""

status_code: int = 422
title: str = "Unprocessable Entity"

def __init__(
self,
status_code: int = 403,
detail: str = None,
headers: dict = None,
title: str = "Forbidden",
) -> None:
super().__init__(status_code=status_code, detail=detail, headers=headers)
self.title = title
super().__init__(status_code=self.status_code, detail=detail, headers=headers)


class InternalServerError(StrReprMixin, HTTPException):
"""500 Internal Server Error"""

status_code: int = 500
title: str = "Internal Server Error"

def __init__(
self,
detail: str = None,
headers: dict = None,
) -> None:
super().__init__(status_code=self.status_code, detail=detail, headers=headers)


class NotImplementedResponse(StrReprMixin, HTTPException):
"""501 Not Implemented"""

status_code: int = 501
title: str = "Not Implemented"

def __init__(
self,
detail: str = None,
headers: dict = None,
) -> None:
super().__init__(status_code=self.status_code, detail=detail, headers=headers)


POSSIBLE_ERRORS = (
BadRequest,
Forbidden,
NotFound,
UnprocessableEntity,
InternalServerError,
NotImplementedResponse,
VersionNotSupported,
)
6 changes: 3 additions & 3 deletions optimade/server/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
StructureResource,
ReferenceResource,
)
from optimade.server.exceptions import POSSIBLE_ERRORS

ENTRY_INFO_SCHEMAS = {
"structures": StructureResource.schema,
"references": ReferenceResource.schema,
}

ERROR_RESPONSES = {
status_code: {"model": ErrorResponse}
for status_code in [400, 403, 404, 422, 500, 501, 553]
err.status_code: {"model": ErrorResponse, "description": err.title}
for err in POSSIBLE_ERRORS
}
ERROR_RESPONSES[553].update({"description": "Version Not Supported"})


def retrieve_queryable_properties(
Expand Down

0 comments on commit 9054d09

Please sign in to comment.