Browse Source

Merge pull request #72 from algoo/feature/flask_error_handler

Bastien Sevajol 6 years ago
parent
commit
379d16ccf3
No account linked to committer's email
2 changed files with 18 additions and 2 deletions
  1. 2 1
      hapic/context.py
  2. 16 1
      hapic/ext/flask/context.py

+ 2 - 1
hapic/context.py View File

206
                             exc,
206
                             exc,
207
                             include_traceback=self.is_debug(),
207
                             include_traceback=self.is_debug(),
208
                         )
208
                         )
209
+                        dumped = error_builder.dump(error_body).data
209
                         return self.get_response(
210
                         return self.get_response(
210
-                            json.dumps(error_body),
211
+                            json.dumps(dumped),
211
                             handled_exception.http_code,
212
                             handled_exception.http_code,
212
                         )
213
                         )
213
                 raise exc
214
                 raise exc

+ 16 - 1
hapic/ext/flask/context.py View File

166
         exception_class: typing.Type[Exception],
166
         exception_class: typing.Type[Exception],
167
         http_code: int,
167
         http_code: int,
168
     ) -> None:
168
     ) -> None:
169
-        raise NotImplementedError('TODO')
169
+        def return_response_error(exc):
170
+            error_builder = self.get_default_error_builder()
171
+            error_body = error_builder.build_from_exception(
172
+                exc,
173
+                include_traceback=self.is_debug(),
174
+            )
175
+            dumped = error_builder.dump(error_body).data
176
+            return self.get_response(
177
+                json.dumps(dumped),
178
+                http_code,
179
+            )
180
+
181
+        self.app.register_error_handler(
182
+            exception_class,
183
+            return_response_error,
184
+        )
170
 
185
 
171
     def is_debug(self) -> bool:
186
     def is_debug(self) -> bool:
172
         return self.debug
187
         return self.debug