Skip to content

Commit

Permalink
Merge pull request #75 from ty-ras/issue/73-add-more-code
Browse files Browse the repository at this point in the history
#73 Adding also the error class.
  • Loading branch information
stazz authored Sep 9, 2023
2 parents ec00d19 + e856ee1 commit 6e157f4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion code/data-frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ty-ras/data-frontend",
"version": "2.1.0",
"version": "2.1.1",
"author": {
"name": "Stanislav Muhametsin",
"email": "[email protected]",
Expand Down
12 changes: 12 additions & 0 deletions code/data-frontend/src/__test__/clients-errors.spec.ts
Original file line number Diff line number Diff line change
@@ -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()));
});
26 changes: 26 additions & 0 deletions code/data-frontend/src/clients-errors.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions code/data-frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./api-call-factory-factory";
export * from "./errors";
export * from "./url";
export * from "./clients";
export * from "./clients-errors";

0 comments on commit 6e157f4

Please sign in to comment.