error-http-response
Version:
Create HTTP error responses
39 lines (29 loc) • 1.05 kB
JavaScript
import normalizeException from"normalize-exception";
import safeJsonValue from"safe-json-value";
import{getOptions}from"./options.js";
const errorHttpResponse=(error,options)=>{
const{name,message,stack,cause,errors,http,...errorProps}=
normalizeException(error);
const optionsA=getOptions(http,options);
return safeJsonValue({
...getOptionalProp(optionsA,"type"),
...getOptionalProp(optionsA,"status"),
...getDefaultedProp(optionsA,"title",String(name)),
...getDefaultedProp(optionsA,"detail",String(message)),
...getOptionalProp(optionsA,"instance"),
...getDefaultedProp(optionsA,"stack",String(stack)),
...getExtra(optionsA,errorProps)
}).value
};
export default errorHttpResponse;
const getOptionalProp=(options,optName)=>
options[optName]===undefined?{}:{[optName]:options[optName]};
const getDefaultedProp=(options,optName,defaultValue)=>({
[optName]:options[optName]??defaultValue
});
const getExtra=({extra},errorProps)=>{
if(extra!==undefined){
return{extra}
}
return Object.keys(errorProps).length===0?{}:{extra:errorProps}
};