mharj/mharj-jwt-util

View on GitHub
src/lib/errorUtil.ts

Summary

Maintainability
A
0 mins
Test Coverage
F
40%
import {z} from 'zod';

export function getError(error: unknown) {
    if (error instanceof Error) {
        return error;
    }
    if (typeof error === 'string') {
        return new Error(error);
    }
    return new TypeError(`Unknown error: ${JSON.stringify(error)}`);
}

export function assertZodError(error: unknown): asserts error is z.ZodError {
    if (!(error instanceof z.ZodError)) {
        throw getError(error);
    }
}