-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy patherrors.py
More file actions
27 lines (21 loc) · 809 Bytes
/
errors.py
File metadata and controls
27 lines (21 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import Optional, Dict
from flask import jsonify
class JSONAPIError(Exception):
def __init__(self, title: str, status: int = 500, code: Optional[str] = None, detail: Optional[str] = None,
source: Optional[Dict[str, str]] = None, meta=None, id=None):
self.title = title
self.status = status
self.code = code
self.detail = detail
self.source = source
self.meta = meta
self.id = id
def to_dict(self) -> Dict[str, any]:
res = {'errors': []}
error = {'title': self.title, 'status': self.status}
if self.code is not None:
error['code'] = self.code
if self.detail is not None:
error['detail'] = self.detail
res['errors'].append(error)
return res