Skip to content

Commit 85e02a9

Browse files
authored
display user decoding errors (#261)
* display user decoding errors * fix tests
1 parent 98d95cb commit 85e02a9

File tree

6 files changed

+34
-23
lines changed

6 files changed

+34
-23
lines changed

src/controllers/__tests__/authenticationController.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ describe("AuthenticationController#acs", () => {
250250

251251
expect(res.json).toHaveBeenCalledWith({
252252
...anErrorResponse,
253-
detail: "Unable to decode the user"
253+
detail: expect.stringContaining(
254+
"value.email is not a string that represents an email address"
255+
)
254256
});
255257
expect(mockSet).not.toHaveBeenCalled();
256258
});
@@ -340,7 +342,7 @@ describe("AuthenticationController#logout", () => {
340342
expect(res.status).toHaveBeenCalledWith(500);
341343
expect(res.json).toHaveBeenCalledWith({
342344
...anErrorResponse,
343-
detail: "Unable to decode the user"
345+
detail: expect.stringContaining("value.family_name is not a string")
344346
});
345347
});
346348

src/controllers/__tests__/messagesController.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe("MessagesController#getMessagesByUser", () => {
123123
expect(mockGetMessagesByUser).not.toBeCalled();
124124
expect(res.json).toHaveBeenCalledWith({
125125
...anErrorResponse,
126-
detail: "Unable to decode the user"
126+
detail: expect.stringContaining("Cannot extract the user from request")
127127
});
128128
});
129129
});
@@ -178,7 +178,7 @@ describe("MessagesController#getMessage", () => {
178178
expect(mockGetMessage).not.toBeCalled();
179179
expect(res.json).toHaveBeenCalledWith({
180180
...anErrorResponse,
181-
detail: "Unable to decode the user"
181+
detail: expect.stringContaining("Cannot extract the user from request")
182182
});
183183
});
184184
});

src/controllers/__tests__/notificationController.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ describe("NotificationController#createOrUpdateInstallation", () => {
199199

200200
expect(res.json).toHaveBeenCalledWith({
201201
...anErrorResponse,
202-
detail: "Unable to decode the user"
202+
detail: expect.stringContaining(
203+
"value.fiscal_code is not a string that matches the pattern"
204+
)
203205
});
204206
});
205207

src/controllers/__tests__/profileController.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe("ProfileController#getProfile", () => {
138138
expect(mockGetProfile).not.toBeCalled();
139139
expect(res.json).toHaveBeenCalledWith({
140140
...anErrorResponse,
141-
detail: "Unable to decode the user"
141+
detail: expect.stringContaining("Cannot extract the user from request")
142142
});
143143
});
144144
});
@@ -196,7 +196,7 @@ describe("ProfileController#upsertProfile", () => {
196196
expect(mockUpsertProfile).not.toBeCalled();
197197
expect(res.json).toHaveBeenCalledWith({
198198
...anErrorResponse,
199-
detail: "Unable to decode the user"
199+
detail: expect.stringContaining("Cannot extract the user from request")
200200
});
201201
});
202202

src/controllers/__tests__/serviceController.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe("serviceController#getService", () => {
111111
expect(mockGetService).not.toBeCalled();
112112
expect(res.json).toHaveBeenCalledWith({
113113
...anErrorResponse,
114-
detail: "Unable to decode the user"
114+
detail: expect.stringContaining("Cannot extract the user from request")
115115
});
116116
});
117117
});

src/types/user.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import * as express from "express";
77
import { Either, left } from "fp-ts/lib/Either";
88
import { fromNullable, none, Option, some, tryCatch } from "fp-ts/lib/Option";
9-
import * as t from "io-ts";
109
import { number, string } from "io-ts";
10+
import * as t from "io-ts";
11+
import { JSONFromString } from "io-ts-types";
12+
import { readableReport } from "italia-ts-commons/lib/reporters";
1113
import { DOMParser } from "xmldom";
1214
import { log } from "../utils/logger";
1315
import { EmailAddress } from "./api/EmailAddress";
@@ -51,8 +53,6 @@ export const SpidUser = t.interface({
5153

5254
export type SpidUser = t.TypeOf<typeof SpidUser>;
5355

54-
const messageErrorOnDecodeUser = "Unable to decode the user";
55-
5656
/**
5757
* Converts a SPID User to a Proxy User.
5858
*/
@@ -83,7 +83,7 @@ export function toAppUser(
8383
// tslint:disable-next-line:no-any
8484
export function validateSpidUser(value: any): Either<Error, SpidUser> {
8585
if (!value.hasOwnProperty("fiscalNumber")) {
86-
return left(new Error(messageErrorOnDecodeUser));
86+
return left(new Error("Cannot decode a user without a fiscalNumber"));
8787
}
8888

8989
// Remove the international prefix from fiscal number.
@@ -131,8 +131,10 @@ export function validateSpidUser(value: any): Either<Error, SpidUser> {
131131

132132
const result = SpidUser.decode(valueWithDefaultSPIDLevel);
133133

134-
return result.mapLeft(() => {
135-
return new Error(messageErrorOnDecodeUser);
134+
return result.mapLeft(err => {
135+
return new Error(
136+
"Cannot validate SPID user object: " + readableReport(err)
137+
);
136138
});
137139
}
138140

@@ -144,22 +146,27 @@ export function extractUserFromRequest(
144146
): Either<Error, User> {
145147
const result = User.decode(from.user);
146148

147-
return result.mapLeft(() => {
148-
return new Error(messageErrorOnDecodeUser);
149+
// tslint:disable-next-line:no-identical-functions
150+
return result.mapLeft(err => {
151+
return new Error(
152+
"Cannot extract the user from request: " + readableReport(err)
153+
);
149154
});
150155
}
151156

152157
/**
153158
* Extracts a user from a json string.
154159
*/
155160
export function extractUserFromJson(from: string): Either<Error, User> {
156-
const json = JSON.parse(from);
157-
158-
const result = User.decode(json);
159-
160-
return result.mapLeft(() => {
161-
return new Error(messageErrorOnDecodeUser);
162-
});
161+
return JSONFromString.decode(from)
162+
.mapLeft(
163+
err => new Error("Cannot parse the user JSON:" + readableReport(err))
164+
)
165+
.chain(json =>
166+
User.decode(json).mapLeft(
167+
err => new Error("Cannot decode the user JSON: " + readableReport(err))
168+
)
169+
);
163170
}
164171

165172
/**

0 commit comments

Comments
 (0)