Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 9x 9x 9x 112x 9x 103x 9x 109x 9x 7x 7x 9x 5x 5x 9x 2x 2x 9x 90x 90x 9x 9x 9x 2x 2x 9x 1x 1x 9x 3x 1x 2x 2x 9x 9x | const { ISSUE, VERSIONS } = require('../../constants');
const { resolveSchema } = require('./schema.utils');
// Helper to determine which operation outcome to retrieve
let getErrorConstructor = (baseVersion) => {
if (!baseVersion || !Object.prototype.hasOwnProperty.call(VERSIONS, baseVersion)) {
return resolveSchema(VERSIONS['3_0_1'], 'OperationOutcome');
} else {
return resolveSchema(baseVersion, 'OperationOutcome');
}
};
/* eslint-disable no-useless-escape */
let div_content = (severity, diagnostics) =>
'<div xmlns="http://www.w3.org/1999/xhtml"><h1>Operation Outcome</h1><table border="0">' +
`<table border=\"0\"><tr><td style=\"font-weight: bold;\">${severity}</td>` +
`<td><pre>${diagnostics}</pre></td></tr></table></div>`;
/* eslint-enable no-useless-escape */
// Invalid or Missing parameter from request
let invalidParameter = (message, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 400,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, message),
},
issue: {
code: ISSUE.CODE.INVALID,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: message,
},
});
};
// Unauthorized request of some resource
let unauthorized = (message, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 401,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, message || 'Unauthorized request'),
},
issue: {
code: ISSUE.CODE.FORBIDDEN,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: message || '401: Unauthorized request',
},
});
};
let insufficientScope = (message, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 403,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, message || 'Insufficient scope'),
},
issue: {
code: ISSUE.CODE.FORBIDDEN,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: message || '403: Insufficient scope',
},
});
};
let notFound = (message, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 404,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, message || 'Not found'),
},
issue: {
code: ISSUE.CODE.NOT_FOUND,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: message || '404: Not found',
},
});
};
let methodNotAllowed = (message, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 405,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, message || 'Method not allowed'),
},
issue: {
code: ISSUE.CODE.NOT_SUPPORTED,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: message || '405: Method not allowed',
},
});
};
let deleteConflict = (message, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 409,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, message || 'Conflict'),
},
issue: {
code: ISSUE.CODE.CONFLICT,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: message || '409: Conflict',
},
});
};
let deleted = (message, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 410,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, message || 'Resource deleted'),
},
issue: {
code: ISSUE.CODE.NOT_FOUND,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: message || '410: Resource deleted',
},
});
};
/**
* @function customError
* @description Take a custom error from user implementation and return an operation outcome
* @param {Error<statusCode, code, severity, message>} err - error or custom error object
* @param {String} base_version - dstu2 or stu3
* @return {OperationOutcome}
*/
let customError = (err, base_version) => {
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: err.statusCode,
text: {
status: 'generated',
div: div_content(err.severity, err.message),
},
issue: {
code: err.code,
severity: err.severity,
diagnostics: err.message,
},
isCustom: true,
});
};
let internal = (err, base_version) => {
if (err.isCustom) {
return customError(err, base_version);
}
let ErrorConstructor = getErrorConstructor(base_version);
return new ErrorConstructor({
statusCode: 500,
text: {
status: 'generated',
div: div_content(ISSUE.SEVERITY.ERROR, err.message || 'Internal server error'),
},
issue: {
code: ISSUE.CODE.EXCEPTION,
severity: ISSUE.SEVERITY.ERROR,
diagnostics: err.message || '500: Internal server error',
},
});
};
let isServerError = (err, base_version) => err instanceof getErrorConstructor(base_version);
/**
* @name exports
* @static
* @summary Error Configurations
*/
module.exports = {
invalidParameter,
unauthorized,
insufficientScope,
methodNotAllowed,
deleteConflict,
notFound,
deleted,
internal,
customError,
isServerError,
};
|