11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst errcode = require('../index'); 41cb0ef41Sopenharmony_ciconst expect = require('expect.js'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cidescribe('errcode', () => { 71cb0ef41Sopenharmony_ci describe('string as first argument', () => { 81cb0ef41Sopenharmony_ci it('should throw an error', () => { 91cb0ef41Sopenharmony_ci expect(() => { errcode('my message'); }).to.throwError((err) => { 101cb0ef41Sopenharmony_ci expect(err).to.be.a(TypeError); 111cb0ef41Sopenharmony_ci }); 121cb0ef41Sopenharmony_ci }); 131cb0ef41Sopenharmony_ci }); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci describe('error as first argument', () => { 161cb0ef41Sopenharmony_ci it('should accept an error and do nothing', () => { 171cb0ef41Sopenharmony_ci const myErr = new Error('my message'); 181cb0ef41Sopenharmony_ci const err = errcode(myErr); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci expect(err).to.be(myErr); 211cb0ef41Sopenharmony_ci expect(err.hasOwnProperty(err.code)).to.be(false); 221cb0ef41Sopenharmony_ci }); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci it('should accept an error and add a code', () => { 251cb0ef41Sopenharmony_ci const myErr = new Error('my message'); 261cb0ef41Sopenharmony_ci const err = errcode(myErr, 'ESOME'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci expect(err).to.be(myErr); 291cb0ef41Sopenharmony_ci expect(err.code).to.be('ESOME'); 301cb0ef41Sopenharmony_ci }); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci it('should accept an error object and add code & properties', () => { 331cb0ef41Sopenharmony_ci const myErr = new Error('my message'); 341cb0ef41Sopenharmony_ci const err = errcode(myErr, 'ESOME', { foo: 'bar', bar: 'foo' }); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci expect(err).to.be.an(Error); 371cb0ef41Sopenharmony_ci expect(err.code).to.be('ESOME'); 381cb0ef41Sopenharmony_ci expect(err.foo).to.be('bar'); 391cb0ef41Sopenharmony_ci expect(err.bar).to.be('foo'); 401cb0ef41Sopenharmony_ci }); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci it('should create an error object without code but with properties', () => { 431cb0ef41Sopenharmony_ci const myErr = new Error('my message'); 441cb0ef41Sopenharmony_ci const err = errcode(myErr, { foo: 'bar', bar: 'foo' }); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci expect(err).to.be.an(Error); 471cb0ef41Sopenharmony_ci expect(err.code).to.be(undefined); 481cb0ef41Sopenharmony_ci expect(err.foo).to.be('bar'); 491cb0ef41Sopenharmony_ci expect(err.bar).to.be('foo'); 501cb0ef41Sopenharmony_ci }); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci it('should set a non-writable field', () => { 531cb0ef41Sopenharmony_ci const myErr = new Error('my message'); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci Object.defineProperty(myErr, 'code', { 561cb0ef41Sopenharmony_ci value: 'derp', 571cb0ef41Sopenharmony_ci writable: false, 581cb0ef41Sopenharmony_ci }); 591cb0ef41Sopenharmony_ci const err = errcode(myErr, 'ERR_WAT'); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci expect(err).to.be.an(Error); 621cb0ef41Sopenharmony_ci expect(err.stack).to.equal(myErr.stack); 631cb0ef41Sopenharmony_ci expect(err.code).to.be('ERR_WAT'); 641cb0ef41Sopenharmony_ci }); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci it('should add a code to frozen object', () => { 671cb0ef41Sopenharmony_ci const myErr = new Error('my message'); 681cb0ef41Sopenharmony_ci const err = errcode(Object.freeze(myErr), 'ERR_WAT'); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci expect(err).to.be.an(Error); 711cb0ef41Sopenharmony_ci expect(err.stack).to.equal(myErr.stack); 721cb0ef41Sopenharmony_ci expect(err.code).to.be('ERR_WAT'); 731cb0ef41Sopenharmony_ci }); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci it('should to set a field that throws at assignment time', () => { 761cb0ef41Sopenharmony_ci const myErr = new Error('my message'); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci Object.defineProperty(myErr, 'code', { 791cb0ef41Sopenharmony_ci enumerable: true, 801cb0ef41Sopenharmony_ci set() { 811cb0ef41Sopenharmony_ci throw new Error('Nope!'); 821cb0ef41Sopenharmony_ci }, 831cb0ef41Sopenharmony_ci get() { 841cb0ef41Sopenharmony_ci return 'derp'; 851cb0ef41Sopenharmony_ci }, 861cb0ef41Sopenharmony_ci }); 871cb0ef41Sopenharmony_ci const err = errcode(myErr, 'ERR_WAT'); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci expect(err).to.be.an(Error); 901cb0ef41Sopenharmony_ci expect(err.stack).to.equal(myErr.stack); 911cb0ef41Sopenharmony_ci expect(err.code).to.be('ERR_WAT'); 921cb0ef41Sopenharmony_ci }); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci it('should retain error type', () => { 951cb0ef41Sopenharmony_ci const myErr = new TypeError('my message'); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci Object.defineProperty(myErr, 'code', { 981cb0ef41Sopenharmony_ci value: 'derp', 991cb0ef41Sopenharmony_ci writable: false, 1001cb0ef41Sopenharmony_ci }); 1011cb0ef41Sopenharmony_ci const err = errcode(myErr, 'ERR_WAT'); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci expect(err).to.be.a(TypeError); 1041cb0ef41Sopenharmony_ci expect(err.stack).to.equal(myErr.stack); 1051cb0ef41Sopenharmony_ci expect(err.code).to.be('ERR_WAT'); 1061cb0ef41Sopenharmony_ci }); 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci it('should add a code to a class that extends Error', () => { 1091cb0ef41Sopenharmony_ci class CustomError extends Error { 1101cb0ef41Sopenharmony_ci set code(val) { 1111cb0ef41Sopenharmony_ci throw new Error('Nope!'); 1121cb0ef41Sopenharmony_ci } 1131cb0ef41Sopenharmony_ci } 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci const myErr = new CustomError('my message'); 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci Object.defineProperty(myErr, 'code', { 1181cb0ef41Sopenharmony_ci value: 'derp', 1191cb0ef41Sopenharmony_ci writable: false, 1201cb0ef41Sopenharmony_ci configurable: false, 1211cb0ef41Sopenharmony_ci }); 1221cb0ef41Sopenharmony_ci const err = errcode(myErr, 'ERR_WAT'); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci expect(err).to.be.a(CustomError); 1251cb0ef41Sopenharmony_ci expect(err.stack).to.equal(myErr.stack); 1261cb0ef41Sopenharmony_ci expect(err.code).to.be('ERR_WAT'); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci // original prototype chain should be intact 1291cb0ef41Sopenharmony_ci expect(() => { 1301cb0ef41Sopenharmony_ci const otherErr = new CustomError('my message'); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci otherErr.code = 'derp'; 1331cb0ef41Sopenharmony_ci }).to.throwError(); 1341cb0ef41Sopenharmony_ci }); 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci it('should support errors that are not Errors', () => { 1371cb0ef41Sopenharmony_ci const err = errcode({ 1381cb0ef41Sopenharmony_ci message: 'Oh noes!', 1391cb0ef41Sopenharmony_ci }, 'ERR_WAT'); 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci expect(err.message).to.be('Oh noes!'); 1421cb0ef41Sopenharmony_ci expect(err.code).to.be('ERR_WAT'); 1431cb0ef41Sopenharmony_ci }); 1441cb0ef41Sopenharmony_ci }); 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci describe('falsy first arguments', () => { 1471cb0ef41Sopenharmony_ci it('should not allow passing null as the first argument', () => { 1481cb0ef41Sopenharmony_ci expect(() => { errcode(null); }).to.throwError((err) => { 1491cb0ef41Sopenharmony_ci expect(err).to.be.a(TypeError); 1501cb0ef41Sopenharmony_ci }); 1511cb0ef41Sopenharmony_ci }); 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci it('should not allow passing undefined as the first argument', () => { 1541cb0ef41Sopenharmony_ci expect(() => { errcode(undefined); }).to.throwError((err) => { 1551cb0ef41Sopenharmony_ci expect(err).to.be.a(TypeError); 1561cb0ef41Sopenharmony_ci }); 1571cb0ef41Sopenharmony_ci }); 1581cb0ef41Sopenharmony_ci }); 1591cb0ef41Sopenharmony_ci}); 160