in source/packages/services/assetlibrary/src/utils/errors.ts [20:116]
export function handleError(e: Error, res: Response): void {
logger.error(`handleError: ${e}`);
let status: number;
let json: unknown = { error: res.statusMessage };
switch (e.name) {
case 'SchemaValidationError': {
status = 400;
json = {
error: res.statusMessage,
errors: (e as SchemaValidationError).errors,
};
break;
}
case 'RelationValidationError': {
status = 400;
const ive = e as RelationValidationError;
json = {
error: res.statusMessage,
invalidDeviceIds: ive.issues.invalidDeviceIds,
invalidGroupPaths: ive.issues.invalidGroupPaths,
invalidRelations: ive.issues.invalidRelations,
};
break;
}
case 'InvalidCategoryError':
case 'InvalidQueryStringError':
case 'ArgumentError':
case 'TypeError':
status = 400;
json = { error: res.statusMessage };
break;
case 'NotAuthorizedError':
status = 403;
break;
case 'NotFoundError':
case 'TemplateNotFoundError':
case 'ProfileNotFoundError':
case 'DeviceNotFoundError':
case 'GroupNotFoundError':
status = 404;
json = { error: res.statusMessage };
break;
case 'TemplateInUseError':
status = 409;
break;
case 'ConditionalCheckFailedException':
case 'ResourceAlreadyExistsException':
status = 409;
json = {
error: 'Item already exists.',
};
break;
case 'NotSupportedError':
status = 501;
break;
default:
if (
e.message.indexOf('with id already exists') >= 0 // thrown by neptune
) {
status = 409;
json = {
error: 'Item already exists.',
};
} else if (
e.hasOwnProperty('code') &&
e['code'] === 'InvalidRequestException' // thrown by IotData in event emitter
) {
status = 400;
} else if (
e.message.indexOf('Unexpected server response: 429') >= 0 // thrown by neptune throttle
) {
status = 429;
json = {
error: 'Too Many Requests',
};
} else if (
e.message.indexOf('TimeLimitExceededException') >= 0 // thrown when large volume of data is taking too long to retrieve
) {
status = 598; // this is the status code returned by the underlying service
json = {
error: 'Underlying service timed out',
};
} else {
status = 500;
}
}
logger.error(`handleError: status:${status}, json:${JSON.stringify(json)}`);
res.status(status).json(json).end();
}