Skip to content

Commit

Permalink
feat(errors): add parseFromMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjozork committed Aug 7, 2022
1 parent 0ca7945 commit d8497df
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ export class FragmenterError extends Error {
return new FragmenterError(code, `FragmenterError(${FragmenterErrorCode[code]}): ${message}`);
}

static parseFromMessage(message: string): FragmenterError {
const regex = /FragmenterError\((\w+)\):\s*(.+)/;

const match = message.match(regex);

if (!match) {
throw new Error('Could not parse FragmenterError: does not match regex');
}

const [, codeString, messageString] = match;

const code = FragmenterErrorCode[codeString as any];

if (typeof code !== 'number') {
throw new Error('Could not parse FragmenterError: unknown code string');
}

return FragmenterError.create(code, messageString);
}

private static interpretNodeException(e: Error): FragmenterErrorCode | null {
const errorCode = (e as unknown as { code: string }).code ?? e.message;

Expand Down

0 comments on commit d8497df

Please sign in to comment.