Skip to content

Better error handling

Latest
Compare
Choose a tag to compare
@exacs exacs released this 11 Feb 09:13
· 28 commits to master since this release

This release comes with some enhancements regarding error handling

Shorter error objects

By default, CanvasApiError thrown by this library contains a property response with a very big object. If users would like to have a smaller response in the error object, they can modify the errorHandler property:

import CanvasApi, { minimalErrorHandler } from "@kth/canvas-api";
const canvas = new CanvasApi("...");
canvas.errorHandler = minimalErrorHandler;

(this shorter error objects might be the default one in the upcoming breaking version)

Custom error objects

Users can also pass a custom function in the .errorHandler property: that function will be called with whatever is thrown by got. Read more about errors in Got here

For example:

import CanvasApi from "@kth/canvas-api";
const canvas = new CanvasApi("...");
canvas.errorHandler = function customHandler(err: unknown) {
  if (err instanceof Error) {
    console.error(err.message);
  }
  throw err;
};