File tree Expand file tree Collapse file tree 3 files changed +17
-10
lines changed
packages/shared/src/errors Expand file tree Collapse file tree 3 files changed +17
-10
lines changed Original file line number Diff line number Diff line change 11import type { ClerkAPIError as ClerkAPIErrorInterface , ClerkAPIErrorJSON } from '@clerk/types' ;
22
3+ import { createErrorTypeGuard } from './createErrorTypeGuard' ;
34import { parseError } from './parseError' ;
45
56export type ClerkApiErrorMeta = Record < string , unknown > ;
@@ -26,6 +27,4 @@ export class ClerkAPIError<Meta extends ClerkApiErrorMeta = any> implements Cler
2627/**
2728 * Type guard to check if a value is a ClerkApiError instance.
2829 */
29- export function isClerkApiError ( error : Error ) : error is ClerkAPIError {
30- return error instanceof ClerkAPIError ;
31- }
30+ export const isClerkApiError = createErrorTypeGuard ( ClerkAPIError ) ;
Original file line number Diff line number Diff line change 1+ import { createErrorTypeGuard } from './createErrorTypeGuard' ;
2+
13export interface ClerkErrorParams {
24 /**
35 * A message that describes the error. This is typically intented to be showed to the developers.
@@ -53,6 +55,8 @@ export class ClerkError extends Error {
5355
5456 this . code = opts . code ;
5557 this . docsUrl = opts . docsUrl ;
58+ this . longMessage = opts . longMessage ;
59+ this . cause = opts . cause ;
5660 }
5761
5862 public toString ( ) {
@@ -64,5 +68,7 @@ export class ClerkError extends Error {
6468 * Type guard to check if a value is a ClerkError instance.
6569 */
6670export function isClerkError ( val : unknown ) : val is ClerkError {
67- return ! ! val && typeof val === 'object' && 'clerkError' in val && val . clerkError === true ;
71+ const typeguard = createErrorTypeGuard ( ClerkError ) ;
72+ // Ths is the base error so we're being more defensive about the type guard
73+ return typeguard ( val ) || ( ! ! val && typeof val === 'object' && 'clerkError' in val && val . clerkError === true ) ;
6874}
Original file line number Diff line number Diff line change 11/* eslint-disable jsdoc/require-jsdoc */
22
3+ type Value = unknown ;
4+
35/**
46 * Creates a type guard function for any error class.
57 * The returned function can be called as a standalone function or as a method on an error object.
1618 * if (error.isMyError()) { ... }
1719 * ```
1820 */
19- export function createErrorTypeGuard < T extends new ( ...args : any [ ] ) => Error > (
21+ export function createErrorTypeGuard < T extends new ( ...args : any [ ] ) => Value > (
2022 ErrorClass : T ,
2123) : {
22- ( error : Error ) : error is InstanceType < T > ;
23- ( this : Error ) : this is InstanceType < T > ;
24+ ( error : Value ) : error is InstanceType < T > ;
25+ ( this : Value ) : this is InstanceType < T > ;
2426} {
25- function typeGuard ( this : Error | void , error ?: Error ) : error is InstanceType < T > {
27+ function typeGuard ( this : Value , error ?: Value ) : error is InstanceType < T > {
2628 const target = error ?? this ;
2729 if ( ! target ) {
2830 throw new TypeError ( `${ ErrorClass . name } type guard requires an error object` ) ;
@@ -31,7 +33,7 @@ export function createErrorTypeGuard<T extends new (...args: any[]) => Error>(
3133 }
3234
3335 return typeGuard as {
34- ( error : Error ) : error is InstanceType < T > ;
35- ( this : Error ) : this is InstanceType < T > ;
36+ ( error : Value ) : error is InstanceType < T > ;
37+ ( this : Value ) : this is InstanceType < T > ;
3638 } ;
3739}
You can’t perform that action at this time.
0 commit comments