Skip to content

Commit 93ca2e5

Browse files
committed
improve error handling
1 parent 541a81d commit 93ca2e5

File tree

8 files changed

+200
-63
lines changed

8 files changed

+200
-63
lines changed

lib/build/error.d.ts

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/error.js

Lines changed: 24 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/index.d.ts

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/index.js

Lines changed: 56 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/supertokens.js

Lines changed: 66 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/error.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
* under the License.
1414
*/
1515

16+
import { logDebugMessage } from "./logger";
17+
1618
export default class SuperTokensError extends Error {
1719
private static errMagic = "ndskajfasndlfkj435234krjdsa";
20+
1821
static BAD_INPUT_ERROR: "BAD_INPUT_ERROR" = "BAD_INPUT_ERROR";
22+
static UNKNOWN_ERROR: "UNKNOWN_ERROR" = "UNKNOWN_ERROR";
1923
static PLUGIN_ERROR: "PLUGIN_ERROR" = "PLUGIN_ERROR";
2024

2125
public type: string;
@@ -43,30 +47,36 @@ export default class SuperTokensError extends Error {
4347
type: "BAD_INPUT_ERROR";
4448
payload: undefined;
4549
}
46-
| {
47-
message: string;
48-
type: "PLUGIN_ERROR";
49-
payload: undefined;
50-
}
5150
) {
5251
super(options.message);
5352
this.type = options.type;
5453
this.payload = options.payload;
5554
this.errMagic = SuperTokensError.errMagic;
5655
}
5756

58-
static isErrorFromSuperTokens(obj: any): obj is SuperTokensError {
57+
static isErrorFromSuperTokens(obj: any): obj is SuperTokensError | SuperTokensPluginError {
5958
return obj.errMagic === SuperTokensError.errMagic;
6059
}
60+
}
6161

62-
static fromError(err: Error, type: string): SuperTokensError {
63-
const newError = new SuperTokensError({
64-
message: err.message,
65-
type,
66-
});
67-
68-
newError.stack = err.stack;
62+
export class SuperTokensPluginError extends SuperTokensError {
63+
public code: number;
6964

70-
return newError;
65+
constructor(options: { message: string; payload?: any; code?: number }) {
66+
super({ ...options, type: SuperTokensError.PLUGIN_ERROR });
67+
this.code = options.code || 400;
7168
}
7269
}
70+
71+
export const transformErrorToSuperTokensError = (err: any): SuperTokensError => {
72+
// passthrough for errors from SuperTokens - let errorHandler handle them
73+
if (SuperTokensError.isErrorFromSuperTokens(err)) {
74+
return err;
75+
}
76+
77+
logDebugMessage(
78+
`transformErrorToSuperTokensError: Transforming error to SuperTokensError. Error: ${err?.message}.\nStack:\n${err?.stack}`
79+
);
80+
// mask the original stack trace by not copying it on the new error
81+
return new SuperTokensError({ message: "Unknown error", type: SuperTokensError.UNKNOWN_ERROR });
82+
};

lib/ts/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import SuperTokens from "./supertokens";
17-
import SuperTokensError from "./error";
17+
import SuperTokensError, { SuperTokensPluginError } from "./error";
1818
import { UserContext, User as UserType } from "./types";
1919
import AccountLinking from "./recipe/accountlinking/recipe";
2020
import { AccountInfoInput } from "./recipe/accountlinking/types";
@@ -36,6 +36,7 @@ export default class SuperTokensWrapper {
3636
static init = SuperTokens.init;
3737

3838
static Error = SuperTokensError;
39+
static PluginError = SuperTokensPluginError;
3940
static RecipeUserId = RecipeUserId;
4041
static User = User;
4142

@@ -243,5 +244,7 @@ export let getAvailableFirstFactors = SuperTokensWrapper.getAvailableFirstFactor
243244

244245
export let Error = SuperTokensWrapper.Error;
245246

247+
export let PluginError = SuperTokensWrapper.PluginError;
248+
246249
export { default as RecipeUserId } from "./recipeUserId";
247250
export { User } from "./user";

0 commit comments

Comments
 (0)