-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): Checks and CodedError classes
Utilities for easier one-liners that can verify arguments or execution state in a much more concise way compared to if conditions throwing manually. Makes the code less verbose and requires less typing as well. Also: added a bools utlity class for checking strict boolean typing of a value and a new string utility method as well. Fixes #266 Signed-off-by: Peter Somogyvari <[email protected]>
- Loading branch information
Showing
5 changed files
with
67 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export class Bools { | ||
/** | ||
* Determines if a value is strictly a boolean `true` or `false`. Anything else | ||
* will result in the method returning `false`. | ||
* | ||
* Useful in cases where you have an optional boolean parameter that you need | ||
* to assign a default value to by determining if it had been set or not. | ||
* | ||
* @param val The value to be decided on whether it's strictly boolean `true` | ||
* or `false`. | ||
*/ | ||
public static isBooleanStrict(val: unknown): boolean { | ||
return val === true || val === false; | ||
} | ||
} |
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,21 @@ | ||
import { CodedError } from "./coded-error"; | ||
|
||
export class Checks { | ||
/** | ||
* Verifies that a boolean condition is met or throws an Error if it is not. | ||
* | ||
* @param checkResult Determines the outcome of the check via it's truthyness. | ||
* @param subjectOfCheck The error message if `checkResult` is falsy. | ||
* @param code The code of the error if `checkResult is falsy. | ||
*/ | ||
public static truthy( | ||
checkResult: any, | ||
subjectOfCheck: string = "variable", | ||
code: string = "-1" | ||
): void { | ||
if (!checkResult) { | ||
const message = `"${subjectOfCheck}" is falsy, need a truthy value.`; | ||
throw new CodedError(message, code); | ||
} | ||
} | ||
} |
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,14 @@ | ||
/** | ||
* Utility class for better exception handling by way of having a code property | ||
* which is designated to uniquely identify the type of exception that was | ||
* thrown, essentially acting as a discriminator property. | ||
*/ | ||
export class CodedError extends Error { | ||
constructor(public readonly message: string, public readonly code: string) { | ||
super(message); | ||
} | ||
|
||
sameCode(codedError: CodedError): boolean { | ||
return this.code === codedError?.code; | ||
} | ||
} |
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
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