Skip to content

Commit 4b7b58a

Browse files
committed
fix: error messages returned by the api
1 parent afd3273 commit 4b7b58a

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

meteor/server/api/rest/v1/index.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ function extractErrorCode(e: unknown): number {
6060
}
6161
}
6262

63-
function extractErrorMessage(e: unknown): string {
63+
function validateUserError(e: unknown): UserError | undefined {
64+
if (UserError.isUserError(e)) {
65+
return e as UserError
66+
}
67+
}
68+
69+
function extractErrorUserMessage(e: unknown): string {
6470
if (ClientAPI.isClientResponseError(e)) {
6571
return translateMessage(e.error.userMessage, interpollateTranslation)
6672
} else if (UserError.isUserError(e)) {
@@ -119,7 +125,7 @@ interface APIRequestError {
119125
function sofieAPIRequest<API, Params, Body, Response>(
120126
method: 'get' | 'post' | 'put' | 'delete',
121127
route: string,
122-
errMsgs: Map<number, UserErrorMessage[]>,
128+
errMsgFallbacks: Map<number, UserErrorMessage[]>,
123129
serverAPIFactory: APIFactory<API>,
124130
handler: (
125131
serverAPI: API,
@@ -140,21 +146,35 @@ function sofieAPIRequest<API, Params, Body, Response>(
140146
ctx.params as unknown as Params,
141147
ctx.request.body as unknown as Body
142148
)
143-
if (ClientAPI.isClientResponseError(response)) throw response.error
149+
if (ClientAPI.isClientResponseError(response)) {
150+
// We wrap our error in another error so the status code override is not lost
151+
throw UserError.from(response.error.rawError, response.error.key, undefined, response.errorCode)
152+
}
144153
ctx.body = JSON.stringify({ status: response.success, result: response.result })
145154
ctx.status = response.success
146155
} catch (e) {
147-
const errCode = extractErrorCode(e)
148-
let errMsg = extractErrorMessage(e)
149-
const msgs = errMsgs.get(errCode)
150-
if (msgs) {
156+
const userError = validateUserError(e)
157+
const errCode = extractErrorCode(userError)
158+
let errMsg = extractErrorUserMessage(userError)
159+
// Get the fallback messages of the endpoint
160+
const fallbackMsgs = errMsgFallbacks.get(errCode)
161+
162+
if (userError?.rawError.message) {
163+
// If we have a detailed arbitrary error message then return that together with the standard error message.
164+
const translatableMessage = {
165+
key: `${errMsg} - ${userError?.rawError.message}`,
166+
}
167+
errMsg = translateMessage(translatableMessage, interpollateTranslation)
168+
} else if (fallbackMsgs) {
169+
// If no detailed error message is provided then return the fallback error messages.
151170
const msgConcat = {
152-
key: msgs
171+
key: fallbackMsgs
153172
.map((msg) => UserError.create(msg, undefined, errCode).userMessage.key)
154-
.reduce((acc, msg) => acc + (acc.length ? ' or ' : '') + msg, ''),
173+
.reduce((acc, msg) => acc + (acc.length ? ' or ' : '') + msg, errMsg),
155174
}
156175
errMsg = translateMessage(msgConcat, interpollateTranslation)
157176
} else {
177+
// Log unknown error codes
158178
logger.error(
159179
`${method.toUpperCase()} for route ${route} returned unexpected error code ${errCode} - ${errMsg}`
160180
)

0 commit comments

Comments
 (0)