Skip to content

Commit 9f30345

Browse files
committed
refactor: make UserUpsertError extend ControllerError
Since we already have this base error we can keep it DRY.
1 parent 547ec26 commit 9f30345

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/controllers/UserController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
BaseResponse,
1616
UserResponse,
1717
} from "../types/api.js";
18-
import { UserUpsertError } from "../lib/users/errors.js";
18+
import { isUserUpsertError } from "../lib/users/errors.js";
1919
import { USER_UPDATE_REQUEST_SCHEMA } from "../lib/users/schemas.js";
2020
import { createStrategy } from "../lib/users/UserUpsertStrategy.js";
2121
import { ParseError } from "../lib/errors/request-parsing.js";
@@ -69,8 +69,8 @@ export class UserController extends Controller {
6969
}
7070

7171
errorResponse(error: unknown) {
72-
if (error instanceof UserUpsertError) {
73-
this.setStatus(error.code);
72+
if (isUserUpsertError(error)) {
73+
this.setStatus(error.statusCode);
7474
return {
7575
success: false,
7676
message: error.message,

src/lib/users/MultisigUpsertStrategy.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ export default class MultisigUpdateStrategy implements UserUpsertStrategy {
6060
if (!parseResult.success) {
6161
throw new UserUpsertError(
6262
400,
63-
parseResult.error.errors.map((e) => e.message).join("; "),
63+
"Couldn't parse user update message",
64+
parseResult.error.errors.reduce((acc, err) => {
65+
const path = err.path.join(".");
66+
return {
67+
...acc,
68+
[path]: err.message,
69+
};
70+
}, {}),
6471
);
6572
}
6673
console.log("Creating signature request for", parseResult);

src/lib/users/errors.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { UserResponse } from "../../types/api.js";
1+
import { ControllerError } from "../errors/controller.js";
22

3-
export class UserUpsertError extends Error {
4-
code: number;
5-
public errors: UserResponse["errors"];
6-
constructor(code: number, message: string) {
7-
super(message);
3+
export class UserUpsertError extends ControllerError {
4+
constructor(code: number, message: string, errors?: Record<string, unknown>) {
5+
super(message, code, errors);
86
this.name = "UserUpdateError";
9-
this.code = code;
107
}
118
}
9+
10+
export function isUserUpsertError(error: unknown): error is UserUpsertError {
11+
return error !== null && error instanceof UserUpsertError;
12+
}

0 commit comments

Comments
 (0)