Skip to content

Commit

Permalink
feat: error consistency improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Sep 28, 2024
1 parent a28471d commit aee34f3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 67 deletions.
10 changes: 5 additions & 5 deletions lib/build/recipe/oauth2provider/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Wrapper {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
static getOAuth2Clients(
Expand All @@ -37,7 +37,7 @@ export default class Wrapper {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
static createOAuth2Client(
Expand All @@ -51,7 +51,7 @@ export default class Wrapper {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
static updateOAuth2Client(
Expand All @@ -65,7 +65,7 @@ export default class Wrapper {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
static deleteOAuth2Client(
Expand All @@ -78,7 +78,7 @@ export default class Wrapper {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
static validateOAuth2AccessToken(
Expand Down
70 changes: 44 additions & 26 deletions lib/build/recipe/oauth2provider/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,20 @@ function getRecipeInterface(
userContext: input.userContext,
});
if (clientInfo.status === "ERROR") {
throw new Error(clientInfo.error);
return {
statusCode: 400,
error: clientInfo.error,
errorDescription: clientInfo.errorDescription,
};
}
const client = clientInfo.client;
const user = await __1.getUser(input.session.getUserId());
if (!user) {
throw new Error("User not found");
return {
statusCode: 400,
error: "invalid_request",
errorDescription: "User deleted",
};
}
// These default to an empty objects, because we want to keep them as a required input
// but they'll not be actually used in the flows where we are not building them.
Expand Down Expand Up @@ -360,7 +368,7 @@ function getRecipeInterface(
return {
statusCode: 400,
error: clientInfo.error,
errorDescription: clientInfo.errorHint,
errorDescription: clientInfo.errorDescription,
};
}
const client = clientInfo.client;
Expand Down Expand Up @@ -400,13 +408,17 @@ function getRecipeInterface(
return {
statusCode: 400,
error: clientInfo.error,
errorDescription: clientInfo.errorHint,
errorDescription: clientInfo.errorDescription,
};
}
const client = clientInfo.client;
const user = await __1.getUser(tokenInfo.sub);
if (!user) {
throw new Error("User not found");
return {
statusCode: 400,
error: "invalid_request",
errorDescription: "User not found",
};
}
body["id_token"] = await this.buildIdTokenPayload({
user,
Expand Down Expand Up @@ -462,7 +474,7 @@ function getRecipeInterface(
return {
status: "ERROR",
error: response.body.error,
errorHint: response.body.errorHint,
errorDescription: response.body.errorDescription,
};
}
},
Expand All @@ -473,31 +485,37 @@ function getRecipeInterface(
{},
input.userContext
);
return {
status: "OK",
client: OAuth2Client_1.OAuth2Client.fromAPIResponse(response.body),
};
// return {
// status: "ERROR",
// error: response.body.error,
// errorHint: response.body.errorHint,
// };
if (response.body.status === "OK") {
return {
status: "OK",
client: OAuth2Client_1.OAuth2Client.fromAPIResponse(response.body),
};
} else {
return {
status: "ERROR",
error: response.body.error,
errorDescription: response.body.errorDescription,
};
}
},
createOAuth2Client: async function (input) {
let response = await querier.sendPostRequest(
new normalisedURLPath_1.default(`/recipe/oauth/clients`),
copyAndCleanRequestBodyInput(input),
input.userContext
);
return {
status: "OK",
client: OAuth2Client_1.OAuth2Client.fromAPIResponse(response),
};
// return {
// status: "ERROR",
// error: response.error,
// errorHint: response.errorHint,
// };
if (response.status === "OK") {
return {
status: "OK",
client: OAuth2Client_1.OAuth2Client.fromAPIResponse(response),
};
} else {
return {
status: "ERROR",
error: response.error,
errorDescription: response.errorDescription,
};
}
},
updateOAuth2Client: async function (input) {
let response = await querier.sendPutRequest(
Expand All @@ -515,7 +533,7 @@ function getRecipeInterface(
return {
status: "ERROR",
error: response.error,
errorHint: response.errorHint,
errorDescription: response.errorDescription,
};
}
},
Expand All @@ -531,7 +549,7 @@ function getRecipeInterface(
return {
status: "ERROR",
error: response.error,
errorHint: response.errorHint,
errorDescription: response.errorDescription,
};
}
},
Expand Down
10 changes: 5 additions & 5 deletions lib/build/recipe/oauth2provider/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export declare type RecipeInterface = {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
getOAuth2Clients(
Expand All @@ -187,7 +187,7 @@ export declare type RecipeInterface = {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
createOAuth2Client(
Expand All @@ -202,7 +202,7 @@ export declare type RecipeInterface = {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
updateOAuth2Client(
Expand All @@ -217,7 +217,7 @@ export declare type RecipeInterface = {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
deleteOAuth2Client(
Expand All @@ -231,7 +231,7 @@ export declare type RecipeInterface = {
| {
status: "ERROR";
error: string;
errorHint: string;
errorDescription: string;
}
>;
validateOAuth2AccessToken(input: {
Expand Down
70 changes: 44 additions & 26 deletions lib/ts/recipe/oauth2provider/recipeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,21 @@ export default function getRecipeInterface(
});

if (clientInfo.status === "ERROR") {
throw new Error(clientInfo.error);
return {
statusCode: 400,
error: clientInfo.error,
errorDescription: clientInfo.errorDescription,
};
}
const client = clientInfo.client;

const user = await getUser(input.session.getUserId());
if (!user) {
throw new Error("User not found");
return {
statusCode: 400,
error: "invalid_request",
errorDescription: "User deleted",
};
}

// These default to an empty objects, because we want to keep them as a required input
Expand Down Expand Up @@ -354,7 +362,7 @@ export default function getRecipeInterface(
return {
statusCode: 400,
error: clientInfo.error,
errorDescription: clientInfo.errorHint,
errorDescription: clientInfo.errorDescription,
};
}
const client = clientInfo.client;
Expand Down Expand Up @@ -393,13 +401,17 @@ export default function getRecipeInterface(
return {
statusCode: 400,
error: clientInfo.error,
errorDescription: clientInfo.errorHint,
errorDescription: clientInfo.errorDescription,
};
}
const client = clientInfo.client;
const user = await getUser(tokenInfo.sub as string);
if (!user) {
throw new Error("User not found");
return {
statusCode: 400,
error: "invalid_request",
errorDescription: "User not found",
};
}
body["id_token"] = await this.buildIdTokenPayload({
user,
Expand Down Expand Up @@ -460,7 +472,7 @@ export default function getRecipeInterface(
return {
status: "ERROR",
error: response.body.error,
errorHint: response.body.errorHint,
errorDescription: response.body.errorDescription,
};
}
},
Expand All @@ -472,15 +484,18 @@ export default function getRecipeInterface(
input.userContext
);

return {
status: "OK",
client: OAuth2Client.fromAPIResponse(response.body),
};
// return {
// status: "ERROR",
// error: response.body.error,
// errorHint: response.body.errorHint,
// };
if (response.body.status === "OK") {
return {
status: "OK",
client: OAuth2Client.fromAPIResponse(response.body),
};
} else {
return {
status: "ERROR",
error: response.body.error,
errorDescription: response.body.errorDescription,
};
}
},
createOAuth2Client: async function (input) {
let response = await querier.sendPostRequest(
Expand All @@ -489,15 +504,18 @@ export default function getRecipeInterface(
input.userContext
);

return {
status: "OK",
client: OAuth2Client.fromAPIResponse(response),
};
// return {
// status: "ERROR",
// error: response.error,
// errorHint: response.errorHint,
// };
if (response.status === "OK") {
return {
status: "OK",
client: OAuth2Client.fromAPIResponse(response),
};
} else {
return {
status: "ERROR",
error: response.error,
errorDescription: response.errorDescription,
};
}
},
updateOAuth2Client: async function (input) {
let response = await querier.sendPutRequest(
Expand All @@ -516,7 +534,7 @@ export default function getRecipeInterface(
return {
status: "ERROR",
error: response.error,
errorHint: response.errorHint,
errorDescription: response.errorDescription,
};
}
},
Expand All @@ -533,7 +551,7 @@ export default function getRecipeInterface(
return {
status: "ERROR",
error: response.error,
errorHint: response.errorHint,
errorDescription: response.errorDescription,
};
}
},
Expand Down
Loading

0 comments on commit aee34f3

Please sign in to comment.