voucherifyio / voucherify-nodejs-sdk

[Deprecated] Node.js SDK for Voucherify - coupons, vouchers, promo codes
http://www.voucherify.io
MIT License
27 stars 16 forks source link

enhancement(error): throw error objects instead of object literals #124

Closed AbdelrahmanHafez closed 4 years ago

AbdelrahmanHafez commented 4 years ago

Throwing object/string literals is considered a bad practice, and we've had an incident today that would've been avoided if voucherify-sdk was throwing error objects instead of object literals because error objects offer useful fields for debugging such as stack it makes things easier.

This PR makes the SDK throw errors that extend the JS error constructor and copy all the fields from the body to the instance error, making all tests pass.

One thing to consider is the fact that in line 8 in VoucherifyError.js we're assigning all values from body to this which can overwrite one of the instance properties:

message
description
number

name
fileName
lineNumber
columnNumber
stack

I suppose overwriting the first 3 is okay and is intended, however overwriting the stack could be undesired since it's basically the main purpose of this PR.

frakti commented 4 years ago

@AbdelrahmanHafez Thank you for commitment. We agree We will include this in next release.

AbdelrahmanHafez commented 4 years ago

Happy to help