3
3
API specification requires custom messages upon error.
4
4
"""
5
5
6
- import json
6
+ import ujson
7
7
from typing import Dict
8
8
from aiohttp import web
9
9
from .. import __apiVersion__
@@ -35,7 +35,17 @@ def process_exception_data(request: Dict, host: str, error_code: int, error: str
35
35
# include datasetIds only if they are specified
36
36
# as per specification if they don't exist all datatsets will be queried
37
37
# Only one of `alternateBases` or `variantType` is required, validated by schema
38
- oneof_fields = ["alternateBases" , "variantType" , "start" , "end" , "startMin" , "startMax" , "endMin" , "endMax" , "datasetIds" ]
38
+ oneof_fields = [
39
+ "alternateBases" ,
40
+ "variantType" ,
41
+ "start" ,
42
+ "end" ,
43
+ "startMin" ,
44
+ "startMax" ,
45
+ "endMin" ,
46
+ "endMax" ,
47
+ "datasetIds" ,
48
+ ]
39
49
data ["alleleRequest" ].update ({k : request .get (k ) for k in oneof_fields if k in request })
40
50
41
51
return data
@@ -51,7 +61,7 @@ class BeaconBadRequest(web.HTTPBadRequest):
51
61
def __init__ (self , request : Dict , host : str , error : str ) -> None :
52
62
"""Return custom bad request exception."""
53
63
data = process_exception_data (request , host , 400 , error )
54
- super ().__init__ (text = json .dumps (data ), content_type = "application/json" )
64
+ super ().__init__ (text = ujson .dumps (data , escape_forward_slashes = False ), content_type = "application/json" )
55
65
LOG .error (f"401 ERROR MESSAGE: { error } " )
56
66
57
67
@@ -65,14 +75,10 @@ class BeaconUnauthorised(web.HTTPUnauthorized):
65
75
def __init__ (self , request : Dict , host : str , error : str , error_message : str ) -> None :
66
76
"""Return custom unauthorized exception."""
67
77
data = process_exception_data (request , host , 401 , error )
68
- headers_401 = {
69
- "WWW-Authenticate" : f'Bearer realm="{ CONFIG_INFO .url } "\n \
70
- error="{ error } "\n \
71
- error_description="{ error_message } "'
72
- }
78
+ headers_401 = {"WWW-Authenticate" : f"""Bearer realm=\" { CONFIG_INFO .url } \" ,error=\" { error } ,\" error_description=\" { error_message } \" """ }
73
79
super ().__init__ (
74
80
content_type = "application/json" ,
75
- text = json .dumps (data ),
81
+ text = ujson .dumps (data , escape_forward_slashes = False ),
76
82
# we use auth scheme Bearer by default
77
83
headers = headers_401 ,
78
84
)
@@ -90,7 +96,7 @@ class BeaconForbidden(web.HTTPForbidden):
90
96
def __init__ (self , request : Dict , host : str , error : str ) -> None :
91
97
"""Return custom forbidden exception."""
92
98
data = process_exception_data (request , host , 403 , error )
93
- super ().__init__ (content_type = "application/json" , text = json .dumps (data ))
99
+ super ().__init__ (content_type = "application/json" , text = ujson .dumps (data , escape_forward_slashes = False ))
94
100
LOG .error (f"403 ERROR MESSAGE: { error } " )
95
101
96
102
@@ -103,5 +109,5 @@ class BeaconServerError(web.HTTPInternalServerError):
103
109
def __init__ (self , error : str ) -> None :
104
110
"""Return custom forbidden exception."""
105
111
data = {"errorCode" : 500 , "errorMessage" : error }
106
- super ().__init__ (content_type = "application/json" , text = json .dumps (data ))
112
+ super ().__init__ (content_type = "application/json" , text = ujson .dumps (data , escape_forward_slashes = False ))
107
113
LOG .error (f"500 ERROR MESSAGE: { error } " )
0 commit comments