HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode-site/node_modules/npm/node_modules/err-code/test/test.js
'use strict';

var errcode = require('../index');
var expect = require('expect.js');

describe('errcode', function () {
    describe('string as first argument', function () {
        it('should create an error object without code', function () {
            var err = errcode('my message');

            expect(err).to.be.an(Error);
            expect(err.hasOwnProperty(err.code)).to.be(false);
        });

        it('should create an error object with code', function () {
            var err = errcode('my message', 'ESOME');

            expect(err).to.be.an(Error);
            expect(err.code).to.be('ESOME');
        });

        it('should create an error object with code and properties', function () {
            var err = errcode('my message', 'ESOME', { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be('ESOME');
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });

        it('should create an error object without code but with properties', function () {
            var err = errcode('my message', { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be(undefined);
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });
    });

    describe('error as first argument', function () {
        it('should accept an error and do nothing', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr);

            expect(err).to.be(myErr);
            expect(err.hasOwnProperty(err.code)).to.be(false);
        });

        it('should accept an error and add a code', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr, 'ESOME');

            expect(err).to.be(myErr);
            expect(err.code).to.be('ESOME');
        });

        it('should accept an error object and add code & properties', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr, 'ESOME', { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be('ESOME');
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });

        it('should create an error object without code but with properties', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr, { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be(undefined);
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });
    });

    it('should allow passing null & undefined in the first argument', function () {
        var err;

        err = errcode(null, 'ESOME');
        expect(err).to.be.an(Error);
        expect(err.message).to.be('null');
        expect(err.code).to.be('ESOME');

        err = errcode(undefined, 'ESOME');
        expect(err).to.be.an(Error);
        expect(err.message).to.be('');
        expect(err.code).to.be('ESOME');
    });
});