Skip to content

Commit

Permalink
fix: remove error handlers in multitenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Jul 12, 2023
1 parent 995b32a commit 97c36ed
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 139 deletions.
2 changes: 1 addition & 1 deletion lib/build/recipe/multitenancy/recipe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Recipe extends RecipeModule {
__: HTTPMethod,
userContext: any
) => Promise<boolean>;
handleError: (err: STError, req: BaseRequest, res: BaseResponse) => Promise<void>;
handleError: (err: STError, _: BaseRequest, __: BaseResponse) => Promise<void>;
getAllCORSHeaders: () => string[];
isErrorFromThisRecipe: (err: any) => err is STError;
}
7 changes: 1 addition & 6 deletions lib/build/recipe/multitenancy/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
16 changes: 0 additions & 16 deletions lib/build/recipe/multitenancy/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
}
export interface RecipeDisabledForTenantErrorHandlerMiddleware {
(message: string, request: BaseRequest, response: BaseResponse): Promise<void>;
}
export interface ErrorHandlers {
onTenantDoesNotExistError?: TenantDoesNotExistErrorHandlerMiddleware;
onRecipeDisabledForTenantError?: RecipeDisabledForTenantErrorHandlerMiddleware;
}
export interface NormalisedErrorHandlers {
onTenantDoesNotExistError: TenantDoesNotExistErrorHandlerMiddleware;
onRecipeDisabledForTenantError: RecipeDisabledForTenantErrorHandlerMiddleware;
}
export declare type TypeInput = {
getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise<string[] | undefined>;
errorHandlers?: ErrorHandlers;
override?: {
functions?: (
originalImplementation: RecipeInterface,
Expand All @@ -30,7 +15,6 @@ export declare type TypeInput = {
};
export declare type TypeNormalisedInput = {
getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise<string[] | undefined>;
errorHandlers: NormalisedErrorHandlers;
override: {
functions: (
originalImplementation: RecipeInterface,
Expand Down
55 changes: 0 additions & 55 deletions lib/build/recipe/multitenancy/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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,
};
}
Expand Down
7 changes: 1 addition & 6 deletions lib/ts/recipe/multitenancy/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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<void> => {
throw err;
};

Expand Down
20 changes: 0 additions & 20 deletions lib/ts/recipe/multitenancy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
}

export interface RecipeDisabledForTenantErrorHandlerMiddleware {
(message: string, request: BaseRequest, response: BaseResponse): Promise<void>;
}

export interface ErrorHandlers {
onTenantDoesNotExistError?: TenantDoesNotExistErrorHandlerMiddleware;
onRecipeDisabledForTenantError?: RecipeDisabledForTenantErrorHandlerMiddleware;
}

export interface NormalisedErrorHandlers {
onTenantDoesNotExistError: TenantDoesNotExistErrorHandlerMiddleware;
onRecipeDisabledForTenantError: RecipeDisabledForTenantErrorHandlerMiddleware;
}

export type TypeInput = {
getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise<string[] | undefined>;

errorHandlers?: ErrorHandlers;
override?: {
functions?: (
originalImplementation: RecipeInterface,
Expand All @@ -52,7 +33,6 @@ export type TypeInput = {
export type TypeNormalisedInput = {
getAllowedDomainsForTenantId?: (tenantId: string, userContext: any) => Promise<string[] | undefined>;

errorHandlers: NormalisedErrorHandlers;
override: {
functions: (
originalImplementation: RecipeInterface,
Expand Down
25 changes: 1 addition & 24 deletions lib/ts/recipe/multitenancy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
};
}
11 changes: 0 additions & 11 deletions