-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from NaturalHistoryMuseum/dev
v0.12.0 release
- Loading branch information
Showing
25 changed files
with
1,755 additions
and
1,738 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,85 @@ | ||
#!/usr/bin/env python3 | ||
# encoding: utf-8 | ||
|
||
from fastapi import HTTPException | ||
import logging | ||
from fastapi import Request | ||
from starlette.responses import JSONResponse | ||
from typing import Optional | ||
|
||
# this is all assuming we're using uvicorn... | ||
uvicorn_logger = logging.getLogger('uvicorn.error') | ||
# use this logger to dump stuff into the same channels as the uvicorn logs | ||
logger = uvicorn_logger.getChild('iiif') | ||
|
||
def profile_not_found() -> HTTPException: | ||
return HTTPException(status_code=404, detail='Profile not recognised') | ||
|
||
class IIIFServerException(Exception): | ||
|
||
def image_not_found() -> HTTPException: | ||
return HTTPException(status_code=404, detail='Image not found') | ||
def __init__(self, public: str, status_code: int = 500, log: Optional[str] = None, | ||
level: int = logging.WARNING, cause: Optional[Exception] = None, | ||
use_public_as_log: bool = True): | ||
super().__init__(public) | ||
self.status_code = status_code | ||
self.public = public | ||
self.log = log | ||
self.level = level | ||
self.cause = cause | ||
if log is not None: | ||
self.log = log | ||
else: | ||
if self.cause is not None: | ||
self.log = f'An error occurred: {self.cause}' | ||
elif use_public_as_log: | ||
self.log = self.public | ||
|
||
|
||
def too_many_images(max_files) -> HTTPException: | ||
return HTTPException(status_code=400, detail=f'Too many images requested (max: {max_files})') | ||
class Timeout(IIIFServerException): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(f'A timeout occurred, please try again', 500, *args, **kwargs) | ||
|
||
def invalid_iiif_parameter(name: str, value: str) -> HTTPException: | ||
return HTTPException(status_code=400, detail=f'{name} value "{value}" is invalid') | ||
|
||
class ProfileNotFound(IIIFServerException): | ||
|
||
def __init__(self, name: str, *args, **kwargs): | ||
super().__init__(f'Profile {name} not recognised', 404, *args, **kwargs) | ||
self.name = name | ||
|
||
|
||
class ImageNotFound(IIIFServerException): | ||
|
||
def __init__(self, profile: str, name: str, *args, **kwargs): | ||
super().__init__(f'Image {name} not found in profile {profile}', 404, *args, | ||
**kwargs) | ||
self.profile = profile | ||
self.name = name | ||
|
||
|
||
class TooManyImages(IIIFServerException): | ||
|
||
def __init__(self, max_files: int, *args, **kwargs): | ||
super().__init__(f'Too many images requested (max: {max_files})', 400, *args, **kwargs) | ||
self.max_files = max_files | ||
|
||
|
||
class InvalidIIIFParameter(IIIFServerException): | ||
|
||
def __init__(self, name: str, value: str, *args, **kwargs): | ||
super().__init__(f'Invalid IIIF option: {name} value "{value}" is invalid', 400, | ||
use_public_as_log=False, *args, **kwargs) | ||
self.name = name | ||
self.value = value | ||
|
||
|
||
async def handler(request: Request, exception: IIIFServerException) -> JSONResponse: | ||
log_error(exception) | ||
return JSONResponse( | ||
status_code=exception.status_code, | ||
content={ | ||
'error': exception.public | ||
}, | ||
) | ||
|
||
|
||
def log_error(exception: IIIFServerException): | ||
if exception.log: | ||
logger.log(exception.level, exception.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.