BairdyB
Denoβ€’3y agoβ€’
5 replies
Bairdy

Deno.serve AbortController onError. How?

When I run my new Deno.serve() updated implementation I get a type error. Perhaps I'm doing it wrong. Maybe it is the way I am trying to use the Abort Controller or maybe the onError handler must always return something?

// You can stop the server with an AbortSignal.
// The abort signal needs to be passed as the signal option in the options bag.
const ac = new AbortController();

const server = Deno.serve({
  // The port to listen on.
  port: config.http.server.port,
  // A literal IP address or host name that can be resolved to an IP address.
  hostname: "0.0.0.0",
  // An AbortSignal to close the server and all connections.
  signal: ac.signal,
  // The callback which is called when the server starts listening.
  onListen({ port, hostname }) {
    // console.log(`Server started at http://${hostname}:${port}`);
    console.log(
      `%cApplication..... fsrv v2.0 - A Deno HTTP Server
Deno v${Deno.version.deno} : Typescript v${Deno.version.typescript} : V8 v${Deno.version.v8}
Gateway URL..... http://${hostname}:${port}
Server Root..... ${pathJoin(CWD, config.http.server.www)}
Internal Pages.. ${pathJoin(CWD, config.http.server.internal)}
Site Root....... ${pathJoin(CWD, config.http.server.public)}`,
      "color: #7986cb",
    );
  },
  // The handler to invoke when route handlers throw an error.
  onError(error) {
    console.error(error);
    log("HTTP 500");

    // FATAL ERROR!

    // The server aborts when the abort signal is sent to the abort controller.
    console.log("Closing server...");
    ac.abort();
    // To wait for the server to close, await the promise returned from the Deno.serve API.
    server.finished.then(() => console.log("Server closed"));

  },
}, handler);


I get the following error:

error: TS2322 [ERROR]: Type '(error: unknown) => void' is not assignable to type '(error: unknown) => Response | Promise<Response>'.
  Type 'void' is not assignable to type 'Response | Promise<Response>'.
  onError(error) {
Was this page helpful?