-
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.
feat: impl and wrap @Nerdware getTypeSafeError
- Loading branch information
1 parent
e780362
commit 51f3b70
Showing
3 changed files
with
47 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
import { getTypeSafeError } from "./getTypeSafeError"; | ||
|
||
describe("getTypeSafeError()", () => { | ||
test("returns an Error instance when called with an Error object", () => { | ||
const result = getTypeSafeError(new Error("test")); | ||
expect(result).toBeInstanceOf(Error); | ||
expect(result.message).toBe("test"); | ||
}); | ||
test("returns an Error instance when called with null", () => { | ||
const result = getTypeSafeError(null, "test"); | ||
expect(result).toBeInstanceOf(Error); | ||
expect(result.message).toBe("test"); | ||
}); | ||
test("returns an Error instance when called with undefined", () => { | ||
const result = getTypeSafeError(undefined, "test"); | ||
expect(result).toBeInstanceOf(Error); | ||
expect(result.message).toBe("test"); | ||
}); | ||
test(`returns an Error instance with "message" set to a string provided as the first argument`, () => { | ||
const result = getTypeSafeError("test"); | ||
expect(result).toBeInstanceOf(Error); | ||
expect(result.message).toBe("test"); | ||
}); | ||
test(`returns an Error instance with a "message" of "An unknown error occurred." when called with an object that doesn't contain a "message" property`, () => { | ||
const input = { test: "test" }; | ||
const result = getTypeSafeError(input); | ||
expect(result).toBeInstanceOf(Error); | ||
expect(result.message).toBe("An unknown error occurred."); | ||
}); | ||
}); |
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,17 @@ | ||
import { isString, getTypeSafeError as _getTypeSafeError } from "@nerdware/ts-type-safety-utils"; | ||
import { ENV } from "@/app/env"; | ||
|
||
/** | ||
* Internal type-safety util which guarantees the returned object is an `Error`. | ||
*/ | ||
export const getTypeSafeError = ( | ||
err: any, | ||
fallBackErrMsg: string = "An unknown error occurred." | ||
): Error => { | ||
return isString(err?.response?.data?.error) // test for AxiosErrors first (// TODO handle this elsewhere, rm this util) | ||
? new Error(err.response.data.error) | ||
: _getTypeSafeError(err, { | ||
fallBackErrMsg, | ||
shouldStringifyUnknownError: !ENV.IS_PROD && !ENV.IS_TEST, | ||
}); | ||
}; |