Skip to content

Commit b788502

Browse files
committed
fix tests
1 parent 8ed495e commit b788502

File tree

8 files changed

+57
-39
lines changed

8 files changed

+57
-39
lines changed

packages/clerk-js/src/core/resources/Verification.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { errorToJSON, parseError } from '@clerk/shared/error';
1+
import { ClerkAPIError, errorToJSON } from '@clerk/shared/error';
22
import type {
3-
ClerkAPIError,
43
PasskeyVerificationResource,
54
PhoneCodeChannel,
65
PublicKeyCredentialCreationOptionsJSON,
@@ -58,7 +57,7 @@ export class Verification extends BaseResource implements VerificationResource {
5857
}
5958
this.attempts = data.attempts;
6059
this.expireAt = unixEpochToDate(data.expire_at || undefined);
61-
this.error = data.error ? parseError(data.error) : null;
60+
this.error = data.error ? new ClerkAPIError(data.error) : null;
6261
this.channel = data.channel || undefined;
6362
}
6463
return this;

packages/clerk-js/src/ui/components/SignUp/__tests__/SignUpContinue.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ClerkAPIResponseError } from '@clerk/shared/error';
22
import { OAUTH_PROVIDERS } from '@clerk/shared/oauth';
33
import { waitFor } from '@testing-library/react';
4-
import React from 'react';
54
import { describe, expect, it } from 'vitest';
65

76
import { bindCreateFixtures } from '@/test/create-fixtures';

packages/shared/src/error.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { errorToJSON, parseError, parseErrors } from './errors/parseError';
22

3+
export { ClerkAPIError } from './errors/clerkApiError';
34
export { ClerkAPIResponseError } from './errors/clerkApiResponseError';
45

56
export { buildErrorThrower, type ErrorThrower, type ErrorThrowerOptions } from './errors/errorThrower';

packages/shared/src/errors/clerkApiError.ts

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

33
import { createErrorTypeGuard } from './createErrorTypeGuard';
4-
import { parseError } from './parseError';
54

65
export type ClerkApiErrorMeta = Record<string, unknown>;
76

@@ -16,11 +15,28 @@ export class ClerkAPIError<Meta extends ClerkApiErrorMeta = any> implements Cler
1615
readonly meta: Meta;
1716

1817
constructor(json: ClerkAPIErrorJSON) {
19-
const parsedError = parseError(json);
18+
const parsedError = this.parseJsonError(json);
2019
this.code = parsedError.code;
2120
this.message = parsedError.message;
2221
this.longMessage = parsedError.longMessage;
23-
this.meta = json.meta as Meta;
22+
this.meta = parsedError.meta;
23+
}
24+
25+
private parseJsonError(json: ClerkAPIErrorJSON) {
26+
return {
27+
code: json.code,
28+
message: json.message,
29+
longMessage: json.long_message,
30+
meta: {
31+
paramName: json.meta?.param_name,
32+
sessionId: json.meta?.session_id,
33+
emailAddresses: json.meta?.email_addresses,
34+
identifiers: json.meta?.identifiers,
35+
zxcvbn: json.meta?.zxcvbn,
36+
plan: json.meta?.plan,
37+
isPlanUpgradePossible: json.meta?.is_plan_upgrade_possible,
38+
} as unknown as Meta,
39+
};
2440
}
2541
}
2642

packages/shared/src/errors/clerkApiResponseError.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface ClerkAPIResponseOptions extends Omit<ClerkErrorParams, 'message' | 'co
1313
}
1414

1515
export class ClerkAPIResponseError extends ClerkError implements ClerkAPIResponseErrorInterface {
16-
name = 'ClerkAPIResponseError';
16+
static name = 'ClerkAPIResponseError';
1717
status: number;
1818
clerkTraceId?: string;
1919
retryAfter?: number;
@@ -22,10 +22,11 @@ export class ClerkAPIResponseError extends ClerkError implements ClerkAPIRespons
2222
constructor(message: string, options: ClerkAPIResponseOptions) {
2323
const { data: errorsJson, status, clerkTraceId, retryAfter } = options;
2424
super({ ...options, message, code: 'api_response_error' });
25+
Object.setPrototypeOf(this, ClerkAPIResponseError.prototype);
2526
this.status = status;
2627
this.clerkTraceId = clerkTraceId;
2728
this.retryAfter = retryAfter;
28-
this.errors = errorsJson.map(e => new ClerkAPIError(e));
29+
this.errors = (errorsJson || []).map(e => new ClerkAPIError(e));
2930
}
3031

3132
public toString() {
@@ -39,6 +40,11 @@ export class ClerkAPIResponseError extends ClerkError implements ClerkAPIRespons
3940

4041
return message;
4142
}
43+
44+
// Override formatMessage to keep it unformatted for backward compatibility
45+
protected static override formatMessage(name: string, msg: string, _: string, __: string | undefined) {
46+
return msg;
47+
}
4248
}
4349

4450
/**

packages/shared/src/errors/clerkError.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,20 @@ export interface ClerkErrorParams {
3434
const __DEV__ = true;
3535

3636
export class ClerkError extends Error {
37+
static name = 'ClerkError';
3738
readonly clerkError = true as const;
38-
readonly name: string = 'ClerkError';
3939
readonly code: string;
4040
readonly longMessage: string | undefined;
4141
readonly docsUrl: string | undefined;
4242
readonly cause: Error | undefined;
4343

44-
constructor(opts: ClerkErrorParams) {
45-
const formatMessage = (msg: string, code: string, docsUrl: string | undefined) => {
46-
msg = `${this.name}: ${msg.trim()}\n\n(code="${code}")\n\n`;
47-
if (__DEV__) {
48-
msg += `\n\nDocs: ${docsUrl}`;
49-
}
50-
return msg;
51-
};
44+
get name() {
45+
return this.constructor.name;
46+
}
5247

53-
super(formatMessage(opts.message, opts.code, opts.docsUrl), { cause: opts.cause });
48+
constructor(opts: ClerkErrorParams) {
49+
super(new.target.formatMessage(new.target.name, opts.message, opts.code, opts.docsUrl), { cause: opts.cause });
5450
Object.setPrototypeOf(this, ClerkError.prototype);
55-
5651
this.code = opts.code;
5752
this.docsUrl = opts.docsUrl;
5853
this.longMessage = opts.longMessage;
@@ -62,6 +57,16 @@ export class ClerkError extends Error {
6257
public toString() {
6358
return `[${this.name}]\nMessage:${this.message}`;
6459
}
60+
61+
protected static formatMessage(name: string, msg: string, code: string, docsUrl: string | undefined) {
62+
console.log('?here2');
63+
64+
msg = `${name}: ${msg.trim()}\n\n(code="${code}")\n\n`;
65+
if (__DEV__ && docsUrl) {
66+
msg += `\n\nDocs: ${docsUrl}`;
67+
}
68+
return msg;
69+
}
6570
}
6671

6772
/**

packages/shared/src/errors/clerkRuntimeError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ type ClerkRuntimeErrorOptions = Omit<ClerkErrorParams, 'message'>;
1313
* throw new ClerkRuntimeError('An error occurred', { code: 'password_invalid' });
1414
*/
1515
export class ClerkRuntimeError extends ClerkError {
16+
static name = 'ClerkRuntimeError';
1617
/**
1718
* @deprecated Use `clerkError` property instead. This property is maintained for backward compatibility.
1819
*/
1920
readonly clerkRuntimeError = true as const;
20-
readonly name = 'ClerkRuntimeError';
2121

2222
constructor(message: string, options: ClerkRuntimeErrorOptions) {
2323
super({ ...options, message });
24+
Object.setPrototypeOf(this, ClerkRuntimeError.prototype);
2425
}
2526
}
2627

packages/shared/src/errors/parseError.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
1-
import type { ClerkAPIError, ClerkAPIErrorJSON } from '@clerk/types';
1+
import type { ClerkAPIError as ClerkAPIErrorInterface, ClerkAPIErrorJSON } from '@clerk/types';
2+
3+
import { ClerkAPIError } from './clerkApiError';
24

35
/**
46
* Parses an array of ClerkAPIErrorJSON objects into an array of ClerkAPIError objects.
57
*
68
* @internal
79
*/
8-
export function parseErrors(data: ClerkAPIErrorJSON[] = []): ClerkAPIError[] {
9-
return data.length > 0 ? data.map(parseError) : [];
10+
export function parseErrors(data: ClerkAPIErrorJSON[] = []): ClerkAPIErrorInterface[] {
11+
return data.length > 0 ? data.map(e => new ClerkAPIError(e)) : [];
1012
}
1113

1214
/**
1315
* Parses a ClerkAPIErrorJSON object into a ClerkAPIError object.
1416
*
17+
* @deprecated Use `ClerkAPIError` class instead
18+
*
1519
* @internal
1620
*/
17-
export function parseError(error: ClerkAPIErrorJSON): ClerkAPIError {
18-
return {
19-
code: error.code,
20-
message: error.message,
21-
longMessage: error.long_message,
22-
meta: {
23-
paramName: error?.meta?.param_name,
24-
sessionId: error?.meta?.session_id,
25-
emailAddresses: error?.meta?.email_addresses,
26-
identifiers: error?.meta?.identifiers,
27-
zxcvbn: error?.meta?.zxcvbn,
28-
plan: error?.meta?.plan,
29-
isPlanUpgradePossible: error?.meta?.is_plan_upgrade_possible,
30-
},
31-
};
21+
export function parseError(error: ClerkAPIErrorJSON): ClerkAPIErrorInterface {
22+
return new ClerkAPIError(error);
3223
}
3324

3425
/**

0 commit comments

Comments
 (0)