Skip to content

Commit bdc1146

Browse files
committed
fix PR comments
1 parent 0c96329 commit bdc1146

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

packages/shared/src/errors/clerkApiError.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ClerkAPIError as ClerkAPIErrorInterface, ClerkAPIErrorJSON } from '@clerk/types';
22

3+
import { createErrorTypeGuard } from './createErrorTypeGuard';
34
import { parseError } from './parseError';
45

56
export 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);

packages/shared/src/errors/clerkError.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createErrorTypeGuard } from './createErrorTypeGuard';
2+
13
export 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
*/
6670
export 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
}

packages/shared/src/errors/createErrorTypeGuard.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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.
@@ -16,13 +18,13 @@
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
}

0 commit comments

Comments
 (0)