From 97c36ed21147b1961dcb36558e660e738224ccea Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Wed, 12 Jul 2023 10:10:22 +0530 Subject: [PATCH] fix: remove error handlers in multitenancy --- lib/build/recipe/multitenancy/recipe.d.ts | 2 +- lib/build/recipe/multitenancy/recipe.js | 7 +-- lib/build/recipe/multitenancy/types.d.ts | 16 ------- lib/build/recipe/multitenancy/utils.js | 55 ----------------------- lib/ts/recipe/multitenancy/recipe.ts | 7 +-- lib/ts/recipe/multitenancy/types.ts | 20 --------- lib/ts/recipe/multitenancy/utils.ts | 25 +---------- test/with-typescript/index.ts | 11 ----- 8 files changed, 4 insertions(+), 139 deletions(-) diff --git a/lib/build/recipe/multitenancy/recipe.d.ts b/lib/build/recipe/multitenancy/recipe.d.ts index 5a50c435a..dbd7ad978 100644 --- a/lib/build/recipe/multitenancy/recipe.d.ts +++ b/lib/build/recipe/multitenancy/recipe.d.ts @@ -30,7 +30,7 @@ export default class Recipe extends RecipeModule { __: HTTPMethod, userContext: any ) => Promise; - handleError: (err: STError, req: BaseRequest, res: BaseResponse) => Promise; + handleError: (err: STError, _: BaseRequest, __: BaseResponse) => Promise; getAllCORSHeaders: () => string[]; isErrorFromThisRecipe: (err: any) => err is STError; } diff --git a/lib/build/recipe/multitenancy/recipe.js b/lib/build/recipe/multitenancy/recipe.js index 3707fc37b..0eb42de96 100644 --- a/lib/build/recipe/multitenancy/recipe.js +++ b/lib/build/recipe/multitenancy/recipe.js @@ -102,13 +102,8 @@ class Recipe extends recipeModule_1.default { } throw new Error("should never come here"); }); - this.handleError = (err, req, res) => + this.handleError = (err, _, __) => __awaiter(this, void 0, void 0, function* () { - if (err.type === "RECIPE_DISABLED_FOR_TENANT_ERROR") { - return yield this.config.errorHandlers.onRecipeDisabledForTenantError(err.message, req, res); - } else if (err.type === "TENANT_DOES_NOT_EXIST_ERROR") { - return yield this.config.errorHandlers.onTenantDoesNotExistError(err.message, req, res); - } throw err; }); this.getAllCORSHeaders = () => { diff --git a/lib/build/recipe/multitenancy/types.d.ts b/lib/build/recipe/multitenancy/types.d.ts index 444f3c2a8..b5f376aa2 100644 --- a/lib/build/recipe/multitenancy/types.d.ts +++ b/lib/build/recipe/multitenancy/types.d.ts @@ -3,23 +3,8 @@ import { BaseRequest, BaseResponse } from "../../framework"; import OverrideableBuilder from "supertokens-js-override"; import { ProviderConfig, ProviderInput } from "../thirdparty/types"; import { GeneralErrorResponse } from "../../types"; -export interface TenantDoesNotExistErrorHandlerMiddleware { - (message: string, request: BaseRequest, response: BaseResponse): Promise; -} -export interface RecipeDisabledForTenantErrorHandlerMiddleware { - (message: string, request: BaseRequest, response: BaseResponse): Promise; -} -export interface ErrorHandlers { - onTenantDoesNotExistError?: TenantDoesNotExistErrorHandlerMiddleware; - onRecipeDisabledForTenantError?: RecipeDisabledForTenantErrorHandlerMiddleware; -} -export interface NormalisedErrorHandlers { - onTenantDoesNotExistError: TenantDoesNotExistErrorHandlerMiddleware; - onRecipeDisabledForTenantError: RecipeDisabledForTenantErrorHandlerMiddleware; -} export declare type TypeInput = { getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise; - errorHandlers?: ErrorHandlers; override?: { functions?: ( originalImplementation: RecipeInterface, @@ -30,7 +15,6 @@ export declare type TypeInput = { }; export declare type TypeNormalisedInput = { getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise; - errorHandlers: NormalisedErrorHandlers; override: { functions: ( originalImplementation: RecipeInterface, diff --git a/lib/build/recipe/multitenancy/utils.js b/lib/build/recipe/multitenancy/utils.js index d116bab0e..7e9613624 100644 --- a/lib/build/recipe/multitenancy/utils.js +++ b/lib/build/recipe/multitenancy/utils.js @@ -13,40 +13,8 @@ * License for the specific language governing permissions and limitations * under the License. */ -var __awaiter = - (this && this.__awaiter) || - function (thisArg, _arguments, P, generator) { - function adopt(value) { - return value instanceof P - ? value - : new P(function (resolve) { - resolve(value); - }); - } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { - try { - step(generator.next(value)); - } catch (e) { - reject(e); - } - } - function rejected(value) { - try { - step(generator["throw"](value)); - } catch (e) { - reject(e); - } - } - function step(result) { - result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); - } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); - }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateAndNormaliseUserInput = void 0; -const utils_1 = require("../../utils"); function validateAndNormaliseUserInput(config) { let override = Object.assign( { @@ -55,32 +23,9 @@ function validateAndNormaliseUserInput(config) { }, config === null || config === void 0 ? void 0 : config.override ); - const errorHandlers = { - onTenantDoesNotExistError: function (message, _, response) { - return __awaiter(this, void 0, void 0, function* () { - utils_1.sendNon200ResponseWithMessage(response, message, 422); - }); - }, - onRecipeDisabledForTenantError: function (message, _, response) { - return __awaiter(this, void 0, void 0, function* () { - utils_1.sendNon200ResponseWithMessage(response, message, 403); - }); - }, - }; - if (config !== undefined) { - if (config.errorHandlers !== undefined) { - if (config.errorHandlers.onTenantDoesNotExistError !== undefined) { - errorHandlers.onTenantDoesNotExistError = config.errorHandlers.onTenantDoesNotExistError; - } - if (config.errorHandlers.onRecipeDisabledForTenantError !== undefined) { - errorHandlers.onRecipeDisabledForTenantError = config.errorHandlers.onRecipeDisabledForTenantError; - } - } - } return { getAllowedDomainsForTenantId: config === null || config === void 0 ? void 0 : config.getAllowedDomainsForTenantId, - errorHandlers, override, }; } diff --git a/lib/ts/recipe/multitenancy/recipe.ts b/lib/ts/recipe/multitenancy/recipe.ts index 06ed0bd13..279c5986d 100644 --- a/lib/ts/recipe/multitenancy/recipe.ts +++ b/lib/ts/recipe/multitenancy/recipe.ts @@ -142,12 +142,7 @@ export default class Recipe extends RecipeModule { throw new Error("should never come here"); }; - handleError = async (err: STError, req: BaseRequest, res: BaseResponse): Promise => { - if (err.type === "RECIPE_DISABLED_FOR_TENANT_ERROR") { - return await this.config.errorHandlers.onRecipeDisabledForTenantError(err.message, req, res); - } else if (err.type === "TENANT_DOES_NOT_EXIST_ERROR") { - return await this.config.errorHandlers.onTenantDoesNotExistError(err.message, req, res); - } + handleError = async (err: STError, _: BaseRequest, __: BaseResponse): Promise => { throw err; }; diff --git a/lib/ts/recipe/multitenancy/types.ts b/lib/ts/recipe/multitenancy/types.ts index ca624bafe..61716a32f 100644 --- a/lib/ts/recipe/multitenancy/types.ts +++ b/lib/ts/recipe/multitenancy/types.ts @@ -18,28 +18,9 @@ import OverrideableBuilder from "supertokens-js-override"; import { ProviderConfig, ProviderInput } from "../thirdparty/types"; import { GeneralErrorResponse } from "../../types"; -export interface TenantDoesNotExistErrorHandlerMiddleware { - (message: string, request: BaseRequest, response: BaseResponse): Promise; -} - -export interface RecipeDisabledForTenantErrorHandlerMiddleware { - (message: string, request: BaseRequest, response: BaseResponse): Promise; -} - -export interface ErrorHandlers { - onTenantDoesNotExistError?: TenantDoesNotExistErrorHandlerMiddleware; - onRecipeDisabledForTenantError?: RecipeDisabledForTenantErrorHandlerMiddleware; -} - -export interface NormalisedErrorHandlers { - onTenantDoesNotExistError: TenantDoesNotExistErrorHandlerMiddleware; - onRecipeDisabledForTenantError: RecipeDisabledForTenantErrorHandlerMiddleware; -} - export type TypeInput = { getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise; - errorHandlers?: ErrorHandlers; override?: { functions?: ( originalImplementation: RecipeInterface, @@ -52,7 +33,6 @@ export type TypeInput = { export type TypeNormalisedInput = { getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise; - errorHandlers: NormalisedErrorHandlers; override: { functions: ( originalImplementation: RecipeInterface, diff --git a/lib/ts/recipe/multitenancy/utils.ts b/lib/ts/recipe/multitenancy/utils.ts index ef75fe505..f5342f6aa 100644 --- a/lib/ts/recipe/multitenancy/utils.ts +++ b/lib/ts/recipe/multitenancy/utils.ts @@ -13,9 +13,7 @@ * under the License. */ -import { TypeInput, TypeNormalisedInput, RecipeInterface, APIInterface, NormalisedErrorHandlers } from "./types"; -import { BaseRequest, BaseResponse } from "../../framework"; -import { sendNon200ResponseWithMessage } from "../../utils"; +import { TypeInput, TypeNormalisedInput, RecipeInterface, APIInterface } from "./types"; export function validateAndNormaliseUserInput(config?: TypeInput): TypeNormalisedInput { let override = { @@ -24,29 +22,8 @@ export function validateAndNormaliseUserInput(config?: TypeInput): TypeNormalise ...config?.override, }; - const errorHandlers: NormalisedErrorHandlers = { - onTenantDoesNotExistError: async function (message: string, _: BaseRequest, response: BaseResponse) { - sendNon200ResponseWithMessage(response, message, 422); - }, - onRecipeDisabledForTenantError: async function (message: string, _: BaseRequest, response: BaseResponse) { - sendNon200ResponseWithMessage(response, message, 403); - }, - }; - - if (config !== undefined) { - if (config.errorHandlers !== undefined) { - if (config.errorHandlers.onTenantDoesNotExistError !== undefined) { - errorHandlers.onTenantDoesNotExistError = config.errorHandlers.onTenantDoesNotExistError; - } - if (config.errorHandlers.onRecipeDisabledForTenantError !== undefined) { - errorHandlers.onRecipeDisabledForTenantError = config.errorHandlers.onRecipeDisabledForTenantError; - } - } - } - return { getAllowedDomainsForTenantId: config?.getAllowedDomainsForTenantId, - errorHandlers, override, }; } diff --git a/test/with-typescript/index.ts b/test/with-typescript/index.ts index 0f36a458b..d52ebec7a 100644 --- a/test/with-typescript/index.ts +++ b/test/with-typescript/index.ts @@ -853,17 +853,6 @@ Multitenancy.init({ }, }); -Multitenancy.init({ - errorHandlers: {}, -}); - -Multitenancy.init({ - errorHandlers: { - onRecipeDisabledForTenantError: async function (message, userContext) {}, - onTenantDoesNotExistError: async function (message, userContext) {}, - }, -}); - Multitenancy.init({ override: { apis: (oI) => {