comparison sat/plugins/plugin_comp_ap_gateway/http_server.py @ 4014:4ef473116499

component AP gateway (http): handle properly NotFound error: When an `exceptions.NotFound` is received, log a warning message and return the appropriate HTTP code instead of raising the default `exceptions.InternalError`.
author Goffi <goffi@goffi.org>
date Sat, 18 Mar 2023 16:53:21 +0100
parents 48e8b3dba793
children 2913313ca58f
comparison
equal deleted inserted replaced
4013:b13673d998dc 4014:4ef473116499
76 if msg is not None: 76 if msg is not None:
77 log.warning(msg) 77 log.warning(msg)
78 request.setResponseCode(http_code, None if msg is None else msg.encode()) 78 request.setResponseCode(http_code, None if msg is None else msg.encode())
79 79
80 def _onRequestError(self, failure_: failure.Failure, request: "HTTPRequest") -> None: 80 def _onRequestError(self, failure_: failure.Failure, request: "HTTPRequest") -> None:
81 log.exception(f"Internal error: {failure_.value}") 81 exc = failure_.value
82 self.responseCode( 82 if isinstance(exc, exceptions.NotFound):
83 request, 83 self.responseCode(
84 http.INTERNAL_SERVER_ERROR, 84 request,
85 f"internal error: {failure_.value}" 85 http.NOT_FOUND,
86 ) 86 str(exc)
87 )
88 else:
89 log.exception(f"Internal error: {failure_.value}")
90 self.responseCode(
91 request,
92 http.INTERNAL_SERVER_ERROR,
93 f"internal error: {failure_.value}"
94 )
95 request.finish()
96 raise failure_
97
87 request.finish() 98 request.finish()
88 raise failure_
89 99
90 async def webfinger(self, request): 100 async def webfinger(self, request):
91 url_parsed = parse.urlparse(request.uri.decode()) 101 url_parsed = parse.urlparse(request.uri.decode())
92 query = parse.parse_qs(url_parsed.query) 102 query = parse.parse_qs(url_parsed.query)
93 resource = query.get("resource", [""])[0] 103 resource = query.get("resource", [""])[0]