diff options
Diffstat (limited to 'ext/http/00_serve.js')
-rw-r--r-- | ext/http/00_serve.js | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index b99f28c0f..5b931ccd1 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -10,6 +10,7 @@ import { Event } from "ext:deno_web/02_event.js"; import { fromInnerResponse, newInnerResponse, + ResponsePrototype, toInnerResponse, } from "ext:deno_fetch/23_response.js"; import { fromInnerRequest, toInnerRequest } from "ext:deno_fetch/23_request.js"; @@ -449,15 +450,26 @@ function mapToCallback(context, callback, onError) { fromInnerRequest(innerRequest, signal, "immutable"), new ServeHandlerInfo(innerRequest), ); + + // Throwing Error if the handler return value is not a Response class + if (!ObjectPrototypeIsPrototypeOf(ResponsePrototype, response)) { + throw TypeError( + "Return value from serve handler must be a response or a promise resolving to a response", + ); + } } catch (error) { try { response = await onError(error); + if (!ObjectPrototypeIsPrototypeOf(ResponsePrototype, response)) { + throw TypeError( + "Return value from onError handler must be a response or a promise resolving to a response", + ); + } } catch (error) { console.error("Exception in onError while handling exception", error); response = internalServerError(); } } - const inner = toInnerResponse(response); if (innerRequest?.[_upgraded]) { // We're done here as the connection has been upgraded during the callback and no longer requires servicing. |