Skip to main content

FileStorage middlewares

withHttpFileStorageError middleware​

The withHttpFileStorageError middleware converts every file error thrown by the wrapped endpoint or downstream middleware into an HttpError, so the request fails with a meaningful status code instead of a generic 500. Errors that are not file errors are rethrown untouched, and the original error is preserved as the cause.

Usage​

./samples/with-http-file-storage-error.ts
import { KeyNotFoundFileError } from "eridu-tech/file-storage/contracts";
import { withHttpFileStorageError } from "eridu-tech/file-storage/middlewares";
import { HttpRouter, defaultHttpRouterAdapter } from "eridu-tech/http-router";

const router = new HttpRouter({
router: defaultHttpRouterAdapter(),
});

router.use(withHttpFileStorageError());

router.endpoint({
url: "/files/1",
method: ["GET"],
handler: async () => {
// A 404 response when the file does not exist
throw KeyNotFoundFileError.create("avatars/1.png");
},
});

Status codes​

ErrorsStatus
InvalidKeyFileError400
KeyNotFoundFileError404
Every UnableTo*FileError, for example UnableToGetPublicUrlFileError and UnableToCopyFileError503

A 400 response means the file key is invalid. A 404 response means the file key does not exist. A 503 response means the file storage adapter failed, so the same request can be retried later. The original error is preserved as the cause, and no identifiers are exposed in the response body.

Further information​

For further information refer to eridu-tech/file-storage API docs.