error-handler

Today’s topic is how to implement a generic error Handler in Deno API server.

It should be executed in middleware in order to catch all errors in one spot. An error Hander usually looks complicated in initial development.

In this article, sharing the simple error handler code. However, it does not exactly follow the original document implementation here. The reason is that I am still not sure the defact standard to use HttpError object by oak properly. The original one returns the error response as content-type: text/plain but we sometimes would like to respond errors as application/json. …


The goal in this article is to get the FrontEnd server start by using Aleph.js which is a React Framework for Deno. Aleph.js is inspired by Next.js.

You can read the official website, here in detail.

Editor

VSCode

Version Infomation
deno 1.6.3

module: aleph@v0.2.28

Installation

To install Aleph, just run the below command so that you can use aleph command on terminal console.

deno install --unstable -A -f -n aleph http://deno.land/x/aleph@v0.2.28/cli.ts

Then, if you want to have a quick start, just run aleph init on the terminal console. The sample project will be generated.

In my case, I wanted to make…


Today, I'm going to share how to get the request body in Deno API Server by oak router module.

Editor

VSCode

Version Infomation
deno 1.8.2 (release, x86_64-apple-darwin)
v8 9.0.257.3
typescript 4.2.2

middleware: oak@v6.5.0

Firstly, you need to import the Router module from oak(in this case version 6.5.0). [the bellow line 5].

Also, you can import RouterContext which shows a router context interface detail in VSCode if you are using TypeScript. [the bellow line 9].


While developing Deno server, I struggled with the middleware implement, especially how to code the several middleware in the root file.

In this article, I code the error handler and logger example.

Version Infomation
deno 1.8.2 (release, x86_64-apple-darwin)
v8 9.0.257.3
typescript 4.2.2

middleware: oak@v6.5.0

log: https://deno.land/std@0.91.0/log/mod.ts

The bellows are the middleware files(error handler, logger examples) and app.ts(root file) examples.

errorHandler.ts

errorHandler.ts

Whenever an error is thrown, this middleware handles the error and responds the error object to a client.

More details about error handling, please check "Deno API Server — How to implement Error Handler by oak".

logger.ts

Taka

I’m Taka, Japanese and living in Japan. I'm planning to emigrate to New Zealand in 7 years with my foreign partner. #Overseas-Emigration #Digital-nomads

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store