From e856ee14d954629d85e9e9381891b861cad9d896 Mon Sep 17 00:00:00 2001 From: Stanislav Muhametsin <346799+stazz@users.noreply.github.com> Date: Sat, 9 Sep 2023 10:49:30 +0300 Subject: [PATCH] #73 Adding also the error class. --- code/data-frontend/package.json | 2 +- .../src/__test__/clients-errors.spec.ts | 12 +++++++++ code/data-frontend/src/clients-errors.ts | 26 +++++++++++++++++++ code/data-frontend/src/index.ts | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 code/data-frontend/src/__test__/clients-errors.spec.ts create mode 100644 code/data-frontend/src/clients-errors.ts diff --git a/code/data-frontend/package.json b/code/data-frontend/package.json index a117ebb..87eda67 100644 --- a/code/data-frontend/package.json +++ b/code/data-frontend/package.json @@ -1,6 +1,6 @@ { "name": "@ty-ras/data-frontend", - "version": "2.1.0", + "version": "2.1.1", "author": { "name": "Stanislav Muhametsin", "email": "346799+stazz@users.noreply.github.com", diff --git a/code/data-frontend/src/__test__/clients-errors.spec.ts b/code/data-frontend/src/__test__/clients-errors.spec.ts new file mode 100644 index 0000000..6804014 --- /dev/null +++ b/code/data-frontend/src/__test__/clients-errors.spec.ts @@ -0,0 +1,12 @@ +/** + * @file This file contains tests for file `../clients-errors.ts`. + */ + +import test from "ava"; +import * as spec from "../clients-errors"; + +test("Validate that isNon2xxStatusCodeError method works", (c) => { + c.plan(2); + c.true(spec.isNon2xxStatusCodeError(new spec.Non2xxStatusCodeError(999))); + c.false(spec.isNon2xxStatusCodeError(new Error())); +}); diff --git a/code/data-frontend/src/clients-errors.ts b/code/data-frontend/src/clients-errors.ts new file mode 100644 index 0000000..eb6ac71 --- /dev/null +++ b/code/data-frontend/src/clients-errors.ts @@ -0,0 +1,26 @@ +/** + * @file This file contains functionality related to errors tha can be thrown during sending the request and receiving response to HTTP backend. + */ + +/** + * This error is thrown when the backend returns something else than `200` or `204` as status code. + * Notice that only type information about this is exported, not the class itself. + */ +export class Non2xxStatusCodeError extends Error { + /** + * Creates new instance of this error with given parameters. + * @param statusCode The status code returned by backend. + */ + public constructor(public readonly statusCode: number) { + super(`Status code ${statusCode} was returned.`); + } +} + +/** + * Helper function to test whether some error is {@link Non2xxStatusCodeError}. + * @param error The {@link Error} to test. + * @returns `true` if given `error` is {@link Non2xxStatusCodeError}, `false` otherwise. + */ +export const isNon2xxStatusCodeError = ( + error: unknown, +): error is Non2xxStatusCodeError => error instanceof Non2xxStatusCodeError; diff --git a/code/data-frontend/src/index.ts b/code/data-frontend/src/index.ts index 2ee745e..621013e 100644 --- a/code/data-frontend/src/index.ts +++ b/code/data-frontend/src/index.ts @@ -8,3 +8,4 @@ export * from "./api-call-factory-factory"; export * from "./errors"; export * from "./url"; export * from "./clients"; +export * from "./clients-errors";