Skip to content

Commit 1e062d9

Browse files
committed
chore: update HTTP API error handling to follow UserError changes
1 parent a8effb8 commit 1e062d9

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,38 +154,30 @@ function sofieAPIRequest<API, Params, Body, Response>(
154154
ctx.body = JSON.stringify({ status: response.success, result: response.result })
155155
ctx.status = response.success
156156
} catch (e) {
157-
console.log('LOOK HERE', e)
158-
159157
const userError = validateUserError(e)
160-
const errCode = extractErrorCode(userError)
161-
let errMsg = extractErrorUserMessage(userError)
158+
const errCode = extractErrorCode(e)
159+
let errMsg = extractErrorUserMessage(e)
162160
// Get the fallback messages of the endpoint
163161
const fallbackMsgs = errMsgFallbacks.get(errCode)
164162

165-
if (userError?.message) {
166-
// If we have a detailed arbitrary error message then return that together with the standard error message.
167-
errMsg = `${translateMessage(
168-
{
169-
key: errMsg,
170-
},
171-
interpollateTranslation
172-
)} - ${userError?.message}`
173-
} else if (fallbackMsgs) {
163+
if (fallbackMsgs && (userError?.message === errMsg || userError?.message === '')) {
174164
// If no detailed error message is provided then return the fallback error messages.
175165
const msgConcat = {
176166
key: fallbackMsgs
177167
.map((msg) => UserError.create(msg, undefined, errCode).userMessage.key)
178168
.reduce((acc, msg) => acc + (acc.length ? ' or ' : '') + msg, errMsg),
179169
}
180170
errMsg = translateMessage(msgConcat, interpollateTranslation)
181-
} else {
182-
// Log unknown error codes
183-
logger.error(
184-
`${method.toUpperCase()} for route ${route} returned unexpected error code ${errCode} - ${errMsg}`
185-
)
171+
} else if (userError?.message) {
172+
// If we have a detailed arbitrary error message then return that together with the standard error message.
173+
errMsg = `${errMsg}${userError.message !== errMsg && userError.message !== '' ? ` - ${userError?.message}` : ''}`
186174
}
187175

188-
logger.error(`${method.toUpperCase()} failed for route ${route}: ${errCode} - ${errMsg}`)
176+
// Log unknown error codes
177+
logger.error(
178+
`${method.toUpperCase()} failed for route ${route}:${!fallbackMsgs ? ' returned unexpected error code' : ''} ${errCode} - ${errMsg}`
179+
)
180+
189181
ctx.type = 'application/json'
190182
const bodyObj: APIRequestError = { status: errCode, message: errMsg }
191183
const details = extractErrorDetails(e)

packages/corelib/src/error.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ export class UserError extends Error {
166166
}
167167
/** Create a UserError duplicating the same error for the log */
168168
static create(key: UserErrorMessage, args?: { [k: string]: any }, errorCode?: number): UserError {
169-
return UserError.from(new Error(UserErrorMessagesTranslations[key]), key, args, errorCode)
169+
return UserError.from(
170+
new Error(translateMessage({ key: UserErrorMessagesTranslations[key], args }, interpollateTranslation)),
171+
key,
172+
args,
173+
errorCode
174+
)
170175
}
171176
static fromSerialized(o: SerializedUserError): UserError {
172177
return new UserError(o.rawError, o.key, o.userMessage, o.errorCode)

0 commit comments

Comments
 (0)