Skip to content

Commit 7a27c4e

Browse files
committed
chore(error): little homogenization
1 parent 115d50c commit 7a27c4e

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

src/config/errors.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
11
export class InvalidEmailError extends Error {
2-
constructor(public didYouMean: string) {
3-
super();
4-
this.didYouMean = didYouMean;
2+
constructor(
3+
public didYouMean: string,
4+
options?: ErrorOptions,
5+
) {
6+
super(`Did you mean "${didYouMean}" ?`, options);
7+
this.name = "InvalidEmailError";
58
}
69
}
710

811
export class ForbiddenError extends Error {}
912

1013
export class UnableToAutoJoinOrganizationError extends Error {
11-
constructor(public moderationId: number) {
12-
super();
13-
this.moderationId = moderationId;
14+
constructor(
15+
public moderationId: number,
16+
options?: ErrorOptions,
17+
) {
18+
super(`Linked to moderation ${moderationId}`, options);
19+
this.name = "UnableToAutoJoinOrganizationError";
1420
}
1521
}
1622

1723
export class UserInOrganizationAlreadyError extends Error {}
1824

1925
export class UserAlreadyAskedToJoinOrganizationError extends Error {
20-
constructor(public moderationId: number) {
21-
super();
22-
this.moderationId = moderationId;
26+
constructor(
27+
public moderationId: number,
28+
options: ErrorOptions,
29+
) {
30+
super(
31+
`Moderation ${moderationId} already asked to join organization`,
32+
options,
33+
);
34+
this.name = "UserAlreadyAskedToJoinOrganizationError";
2335
}
2436
}
2537

2638
export class UserMustConfirmToJoinOrganizationError extends Error {
27-
constructor(public organizationId: number) {
28-
super();
29-
this.organizationId = organizationId;
39+
constructor(
40+
public organizationId: number,
41+
options?: ErrorOptions,
42+
) {
43+
super(`Organization ${organizationId} confirmation is required`, options);
44+
this.name = "UserMustConfirmToJoinOrganizationError";
3045
}
3146
}
3247

@@ -42,7 +57,7 @@ export class DomainRestrictedError extends Error {
4257
public organizationId: number,
4358
options?: ErrorOptions,
4459
) {
45-
super("", options);
60+
super(`Organization ${organizationId} is domain restricted`, options);
4661
this.name = "DomainRestrictedError";
4762
}
4863
}
@@ -109,10 +124,10 @@ export class OidcError extends Error {
109124
constructor(
110125
public error: string,
111126
public error_description?: string,
127+
options?: ErrorOptions,
112128
) {
113-
super();
114-
this.error = error;
115-
this.error_description = error_description;
129+
super(`${error}: ${error_description}`, options);
130+
this.name = "OidcError";
116131
}
117132
}
118133

src/controllers/interaction.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NextFunction, Request, Response } from "express";
2+
import { AssertionError } from "node:assert";
23
import Provider, { errors } from "oidc-provider";
34
import { z } from "zod";
45
import { FEATURE_ALWAYS_RETURN_EIDAS1_FOR_ACR } from "../config/env";
@@ -131,6 +132,13 @@ export const interactionEndControllerFactory =
131132
new OidcError(
132133
"access_denied",
133134
"none of the requested ACRs could be obtained",
135+
{
136+
cause: new AssertionError({
137+
expected: prompt,
138+
actual: currentAcr,
139+
operator: "isAcrSatisfied",
140+
}),
141+
},
134142
),
135143
);
136144
}

src/controllers/user/franceconnect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export async function getFranceConnectLoginCallbackMiddleware(
4040

4141
if (errorQuery.success) {
4242
const { error, error_description } = errorQuery.data;
43-
throw new OidcError(error, error_description);
43+
throw new OidcError(error, error_description, {
44+
cause: errorQuery.error,
45+
});
4446
}
4547
const { code } = await z.object({ code: z.string() }).parseAsync(req.query);
4648

src/managers/organization/join.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
} from "@gouvfr-lasuite/proconnect.identite/types";
2121
import * as Sentry from "@sentry/node";
2222
import { isEmpty, some } from "lodash-es";
23+
import { AssertionError } from "node:assert";
2324
import { inspect } from "node:util";
2425
import {
2526
CRISP_WEBSITE_ID,
@@ -171,7 +172,13 @@ export const joinOrganization = async ({
171172
});
172173
if (!isEmpty(pendingModeration)) {
173174
const { id: moderation_id } = pendingModeration;
174-
throw new UserAlreadyAskedToJoinOrganizationError(moderation_id);
175+
throw new UserAlreadyAskedToJoinOrganizationError(moderation_id, {
176+
cause: new AssertionError({
177+
expected: undefined,
178+
actual: pendingModeration,
179+
operator: "findPendingModeration",
180+
}),
181+
});
175182
}
176183

177184
const { id: organization_id, cached_libelle } = organization;

src/managers/user.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from "@gouvfr-lasuite/proconnect.identite/types";
2929
import { to } from "await-to-js";
3030
import { isEmpty } from "lodash-es";
31+
import { AssertionError } from "node:assert";
3132
import {
3233
FRANCECONNECT_VERIFICATION_MAX_AGE_IN_MINUTES,
3334
HOST,
@@ -93,7 +94,13 @@ export const startLogin = async (
9394
didYouMean = getDidYouMeanSuggestion(email);
9495
}
9596

96-
throw new InvalidEmailError(didYouMean);
97+
throw new InvalidEmailError(didYouMean, {
98+
cause: new AssertionError({
99+
actual: email,
100+
expected: didYouMean,
101+
operator: "isEmailSafeToSendTransactional",
102+
}),
103+
});
97104
}
98105

99106
return {

0 commit comments

Comments
 (0)