-
Notifications
You must be signed in to change notification settings - Fork 10
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 #68 from Protonull/update-metadata
- Loading branch information
Showing
8 changed files
with
217 additions
and
227 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import node_os from 'node:os' | ||
import node_utils from 'node:util' | ||
|
||
export enum ErrorType { | ||
FileExists, | ||
FileNotFound, | ||
UNKNOWN, | ||
} | ||
|
||
/** | ||
* Attempts to transform Node's less-than-helpful exceptions into something | ||
* more readable and logic-able. | ||
*/ | ||
export function getErrorType(error: any): ErrorType { | ||
switch (Math.abs(error.errno ?? Infinity)) { | ||
case node_os.constants.errno.ENOENT: | ||
return ErrorType.FileNotFound | ||
case node_os.constants.errno.EEXIST: | ||
return ErrorType.FileExists | ||
default: | ||
return ErrorType.UNKNOWN | ||
} | ||
} | ||
|
||
/** | ||
* Utility that guarantees that the error is an instance of Error. | ||
*/ | ||
export function ensureError(error: any): Error { | ||
if (error instanceof Error) { | ||
return error | ||
} | ||
switch (typeof error) { | ||
case 'string': | ||
return new Error(error) | ||
case 'number': | ||
case 'bigint': | ||
return new Error(String(error)) | ||
} | ||
return new Error(node_utils.inspect(error)) | ||
} | ||
|
||
/** | ||
* This is useful in cases where you need to throw but can't because of | ||
* Javascript. Read more for context: | ||
* https://www.proposals.es/proposals/throw%20expressions | ||
*/ | ||
export function inlineThrow<T>(error: any): T { | ||
throw 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,16 @@ | ||
export type JSONObject = { [key: string]: JSONValue | undefined } | ||
export type JSONArray = JSONValue[] | ||
export type JSONValue = | ||
| JSONObject | ||
| JSONArray | ||
| string | ||
| number | ||
| boolean | ||
| null | ||
|
||
/** | ||
* Wrapper function for JSON.parse() that provides a proper return type. | ||
*/ | ||
export function parse(raw: string): JSONValue { | ||
return JSON.parse(raw) | ||
} |
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
Oops, something went wrong.