-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from ty-ras/issue/73-add-more-code
#73 Adding also the error class.
- Loading branch information
Showing
4 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters