1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from sanic import Sanic , Blueprint
15+ from sanic import Blueprint , Sanic
1616from sanic .exceptions import NotFound , SanicException , ServerError
1717from sanic .handlers import ErrorHandler
1818from sanic .response import json , stream
2222
2323class MethodView (HTTPMethodView ):
2424 async def get (self , request ):
25- return json ({' hello' : ' world' })
25+ return json ({" hello" : " world" })
2626
2727 post = get
2828 put = get
@@ -33,17 +33,17 @@ async def get(self, request):
3333class CustomErrorHandler (ErrorHandler ):
3434 def response (self , request , exception ):
3535 if isinstance (exception , ZeroDivisionError ):
36- raise ValueError (' Value Error' )
36+ raise ValueError (" Value Error" )
3737 else :
3838 base_response = ErrorHandler .response
39- if hasattr (base_response , ' __wrapped__' ):
39+ if hasattr (base_response , " __wrapped__" ):
4040 base_response = base_response .__wrapped__
4141
4242 return base_response (self , request , exception )
4343
4444 def add (self , exception , handler , * args , ** kwargs ):
4545 base_add = ErrorHandler .add
46- if hasattr (base_add , ' __wrapped__' ):
46+ if hasattr (base_add , " __wrapped__" ):
4747 base_add = base_add .__wrapped__
4848 base_add (self , exception , handler )
4949
@@ -57,13 +57,13 @@ def __init__(self):
5757
5858 def add (self , * args , ** kwargs ):
5959 base_add = Router .add
60- if hasattr (base_add , ' __wrapped__' ):
60+ if hasattr (base_add , " __wrapped__" ):
6161 base_add = base_add .__wrapped__
6262 return base_add .__get__ (self , Router )(* args , ** kwargs )
6363
6464 def get (self , * args ):
6565 base_get = Router .get
66- if hasattr (base_get , ' __wrapped__' ):
66+ if hasattr (base_get , " __wrapped__" ):
6767 base_get = base_get .__wrapped__
6868
6969 if len (args ) == 1 :
@@ -73,80 +73,89 @@ def get(self, *args):
7373
7474 bound_get = base_get .__get__ (self , Router )
7575 get_results = list (bound_get (* args ))
76- if path == ' /server-error' :
76+ if path == " /server-error" :
7777 from sanic .exceptions import ServerError
78+
7879 raise ServerError ("Server Error" )
7980 return get_results
8081
8182
83+ try :
84+ error_handler = CustomErrorHandler (fallback = "text" )
85+ except TypeError :
86+ error_handler = CustomErrorHandler ()
87+
8288router = CustomRouter ()
83- app = Sanic (name = "test app" , error_handler = CustomErrorHandler () , router = router )
89+ app = Sanic (name = "test app" , error_handler = error_handler , router = router )
8490router .app = app
8591blueprint = Blueprint ("test_bp" )
8692
87- @app .route ('/' )
93+
94+ @app .route ("/" )
8895async def index (request ):
89- return json ({' hello' : ' world' })
96+ return json ({" hello" : " world" })
9097
9198
92- @app .route (' /error' )
99+ @app .route (" /error" )
93100async def error (request ):
94- raise ValueError (' Exception' )
101+ raise ValueError (" Exception" )
95102
96103
97104# see write_callback in confest.create_request_coroutine
98- @app .route (' /write_response_error' )
105+ @app .route (" /write_response_error" )
99106async def write_response_error (request ):
100- return json ({' url' : ' write_response_error' })
107+ return json ({" url" : " write_response_error" })
101108
102109
103- @app .route (' /404' )
110+ @app .route (" /404" )
104111async def not_found (request ):
105112 raise NotFound ("Not found" )
106113
107114
108- @app .route (' /zero' )
115+ @app .route (" /zero" )
109116async def zero_division_error (request ):
110117 1 / 0
111118
112119
113- @app .middleware (' request' )
120+ @app .middleware (" request" )
114121async def request_middleware (request ):
115122 return None
116123
117124
118- @blueprint .middleware (' request' )
125+ @blueprint .middleware (" request" )
119126async def blueprint_middleware (request ):
120127 return None
121128
129+
122130# register the middleware a second time, testing that the `request_middleware`
123131# function is not getting double wrapped
124132app .register_middleware (request_middleware )
125133
126134
127- @app .route (' /streaming' )
135+ @app .route (" /streaming" )
128136async def streaming (request ):
129137 async def streaming_fn (response ):
130- response .write ('foo' )
131- response .write ('bar' )
138+ response .write ("foo" )
139+ response .write ("bar" )
140+
132141 return stream (streaming_fn )
133142
134143
135144# Fake websocket endpoint to enable websockets on the server
136- @app .websocket (' /socket' )
145+ @app .websocket (" /socket" )
137146async def socket (request , ws ):
138147 assert False
139148
140149
141- @app .route (' /custom-header/<header_key>/<header_value>' )
150+ @app .route (" /custom-header/<header_key>/<header_value>" )
142151async def custom_header (request , header_key , header_value ):
143152 custom_headers = {header_key : header_value }
144- return json ({' hello' : ' world' }, headers = custom_headers )
153+ return json ({" hello" : " world" }, headers = custom_headers )
145154
146155
147- @app .route (' /server-error' )
156+ @app .route (" /server-error" )
148157async def server_error (request ):
149- raise AssertionError (' This handler should never be reached!' )
158+ raise AssertionError (" This handler should never be reached!" )
150159
151160
152161class CustomExceptionSync (SanicException ):
@@ -164,36 +173,37 @@ def handle_server_error(request, exception):
164173
165174@app .exception (CustomExceptionSync )
166175def handle_custom_exception_sync (request , exception ):
167- raise SanicException (' something went wrong' )
176+ raise SanicException (" something went wrong" )
168177
169178
170179@app .exception (CustomExceptionAsync )
171180async def handle_custom_exception_async (request , exception ):
172- raise SanicException (' something went wrong' )
181+ raise SanicException (" something went wrong" )
173182
174183
175- @app .route (' /sync-error' )
184+ @app .route (" /sync-error" )
176185async def sync_error (request ):
177- raise CustomExceptionSync (' something went wrong' )
186+ raise CustomExceptionSync (" something went wrong" )
178187
179188
180- @app .route (' /async-error' )
189+ @app .route (" /async-error" )
181190async def async_error (request ):
182- raise CustomExceptionAsync (' something went wrong' )
191+ raise CustomExceptionAsync (" something went wrong" )
183192
184193
185- @blueprint .route (' /blueprint' )
194+ @blueprint .route (" /blueprint" )
186195async def blueprint_route (request ):
187196 async def streaming_fn (response ):
188- response .write ('foo' )
197+ response .write ("foo" )
198+
189199 return stream (streaming_fn )
190200
191201
192202app .blueprint (blueprint )
193- app .add_route (MethodView .as_view (), ' /method_view' )
203+ app .add_route (MethodView .as_view (), " /method_view" )
194204
195205if not getattr (router , "finalized" , True ):
196206 router .finalize ()
197207
198- if __name__ == ' __main__' :
199- app .run (host = ' 127.0.0.1' , port = 8000 )
208+ if __name__ == " __main__" :
209+ app .run (host = " 127.0.0.1" , port = 8000 )
0 commit comments