33API specification requires custom messages upon error.
44"""
55
6- import json
6+ import ujson
77from typing import Dict
88from aiohttp import web
99from .. import __apiVersion__
@@ -35,7 +35,17 @@ def process_exception_data(request: Dict, host: str, error_code: int, error: str
3535 # include datasetIds only if they are specified
3636 # as per specification if they don't exist all datatsets will be queried
3737 # 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+ ]
3949 data ["alleleRequest" ].update ({k : request .get (k ) for k in oneof_fields if k in request })
4050
4151 return data
@@ -51,7 +61,7 @@ class BeaconBadRequest(web.HTTPBadRequest):
5161 def __init__ (self , request : Dict , host : str , error : str ) -> None :
5262 """Return custom bad request exception."""
5363 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" )
5565 LOG .error (f"401 ERROR MESSAGE: { error } " )
5666
5767
@@ -65,14 +75,10 @@ class BeaconUnauthorised(web.HTTPUnauthorized):
6575 def __init__ (self , request : Dict , host : str , error : str , error_message : str ) -> None :
6676 """Return custom unauthorized exception."""
6777 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 } \" """ }
7379 super ().__init__ (
7480 content_type = "application/json" ,
75- text = json .dumps (data ),
81+ text = ujson .dumps (data , escape_forward_slashes = False ),
7682 # we use auth scheme Bearer by default
7783 headers = headers_401 ,
7884 )
@@ -90,7 +96,7 @@ class BeaconForbidden(web.HTTPForbidden):
9096 def __init__ (self , request : Dict , host : str , error : str ) -> None :
9197 """Return custom forbidden exception."""
9298 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 ))
94100 LOG .error (f"403 ERROR MESSAGE: { error } " )
95101
96102
@@ -103,5 +109,5 @@ class BeaconServerError(web.HTTPInternalServerError):
103109 def __init__ (self , error : str ) -> None :
104110 """Return custom forbidden exception."""
105111 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 ))
107113 LOG .error (f"500 ERROR MESSAGE: { error } " )
0 commit comments