Based on #103
Users likely expect to be able to pass custom properties to errors when creating them via the shortcut methods such as createError.NotFound().
Proposed change would look something like:
constcreateError=require('http-errors')createError.NotFound({message: "Aw shucks",data: {foo: "bar"}})// does not work todayYou need to use the createError({...}) form to pass custom properties to an error.
createError(404,{message: "Aw shucks",data: {foo: "bar"}})There's a few caveats with the current implementation, namely you can't set status via properties.
createError({status: 404,message: "Aw shucks",data: {foo: "bar"}})// status is ignored, will create a 500 server error with provided message and data fieldsThere is also a very high arity for createError, which should be locked down into a stable interface.
(code)
for(vari=0;i<arguments.length;i++){vararg=arguments[i]vartype=typeofargif(type==='object'&&arginstanceofError){err=argstatus=err.status||err.statusCode||status}elseif(type==='number'&&i===0){status=arg}elseif(type==='string'){msg=arg}elseif(type==='object'){props=arg}else{thrownewTypeError('argument #'+(i+1)+' unsupported type '+type)}}Any of these changes would be semver major. There aren't plans for a major release of http-errors yet, but these are things Im thinking about.
why did I put these both in one issue?
These are both topics related to the interfaces exposed by the library for accepting arguments, which can use some rethinking.
Based on #103
Users likely expect to be able to pass custom properties to errors when creating them via the shortcut methods such as
createError.NotFound().Proposed change would look something like:
You need to use the
createError({...})form to pass custom properties to an error.There's a few caveats with the current implementation, namely you can't set
statusvia properties.There is also a very high arity for
createError, which should be locked down into a stable interface.(code)
Any of these changes would be semver major. There aren't plans for a major release of
http-errorsyet, but these are things Im thinking about.why did I put these both in one issue?
These are both topics related to the interfaces exposed by the library for accepting arguments, which can use some rethinking.