Skip to content

Commit

Permalink
Merge pull request #44 from adrienv1520/develop
Browse files Browse the repository at this point in the history
1.2.9
  • Loading branch information
adrienv1520 authored Feb 15, 2021
2 parents a2aa7ca + 134c20e commit 5c8518a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 1.2.9 - delivery @15/02/2021

- improve LibError error library
- add tests
- fix Readme typo

## 1.2.8 - delivery @23/10/2020

- improve LibError error library
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*Bitgener* is released under the MIT license.

Copyright (C) 2020 Adrien Valcke
Copyright (C) 2021 Adrien Valcke

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
- [Contributing](#contributing)
- [Support](#support)
- [Security](#security)
- [Licence](#licence)
- [License](#license)

# Presentation

Expand Down Expand Up @@ -544,5 +544,5 @@ Please see our [Support](.github/SUPPORT.md) page if you have any questions or f
# Security
For any security concerns or issues, please visit our [Security Policy](.github/SECURITY.md) page.

# Licence
# License
[MIT](LICENSE.md).
33 changes: 25 additions & 8 deletions lib/LibError/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/**
* LibError
*
* - code
* - message
* - name
* - stack
* - originStack
*
* - setCode(value)
* - setMessage(value)
* - setName(value)
* - setOriginStack(originError)
* - toString()
*
* - static get Codes()
* - static format(err) -> LibError
*/
const { str } = require('../helpers/cast');
const { is, exists } = require('../helpers/object');
const Codes = require('./Codes');
Expand All @@ -19,7 +37,7 @@ class LibError extends Error {
this.setName(LibError.Codes.UNKNOWN_ERROR.name);
}

this.setStack(originError);
this.setOriginStack(originError);
}

static get Codes() {
Expand Down Expand Up @@ -53,7 +71,7 @@ class LibError extends Error {
}

// set proper stack trace or keep original
error.setStack(err);
error.setOriginStack(err);
} else {
error = err;
}
Expand Down Expand Up @@ -88,17 +106,16 @@ class LibError extends Error {
return false;
}

setStack(originError) {
setOriginStack(originError) {
if (is(Error, originError) && is(String, originError.stack)) {
this.stack += originError.stack;
return true;
this.originStack = `\n${originError.stack}`;
} else if (this.originStack === undefined) {
this.originStack = '';
}

return false;
}

toString() {
return `${this.name}: code ${this.code}\n${this.stack}`;
return `code ${this.code}\n${this.stack}${this.originStack}`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const generate = async function generate({ svg, settings } = {}) {

svgOutput = svg;
} catch (e) {
const error = new LibError(LibError.Codes.WRITABLE_INTERNAL);
const error = new LibError(LibError.Codes.WRITABLE_INTERNAL, e);
error.setMessage(e.message);

throw error;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bitgener",
"version": "1.2.8",
"version": "1.2.9",
"description": "A lightweight and zero-dependencies pure Node.js barcode generator",
"author": "Adrien Valcke <[email protected]> (https://github.com/adrienv1520)",
"main": "lib/index.js",
Expand Down
24 changes: 17 additions & 7 deletions test/LibError/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('#LibError', function() {
expect(error.constructor.name).to.equals('LibError');
});

it('should return an unknown error with the origin error\'s code/message/name as a message if not a standard error and stack including origin stack', function() {
it('should return an unknown error with the origin error\'s code/message/name as a message if not a standard error and having the origin error stack', function() {
let err = new URIError('uri not available');
let error = LibError.format(err);

Expand All @@ -109,7 +109,9 @@ describe('#LibError', function() {
expect(error.message).to.equals('uri not available');
expect(error.name).to.equals('LibError');
expect(error.constructor.name).to.equals('LibError');
expect(error.stack).to.be.a('string').and.to.include(err.stack);
expect(error.originStack).to.be.a('string').and.to.equals(`\n${err.stack}`);
expect(error.toString()).to.be.a('string').and.to.include(error.stack);
expect(error.toString()).to.be.a('string').and.to.include(err.stack);

err = new URIError();
error = LibError.format(err);
Expand All @@ -119,7 +121,9 @@ describe('#LibError', function() {
expect(error.message).to.equals('URIError');
expect(error.name).to.equals('LibError');
expect(error.constructor.name).to.equals('LibError');
expect(error.stack).to.be.a('string').and.to.include(err.stack);
expect(error.originStack).to.be.a('string').and.to.equals(`\n${err.stack}`);
expect(error.toString()).to.be.a('string').and.to.include(error.stack);
expect(error.toString()).to.be.a('string').and.to.include(err.stack);

err = new Error();
error = LibError.format(err);
Expand All @@ -129,7 +133,9 @@ describe('#LibError', function() {
expect(error.message).to.equals('unknown error');
expect(error.name).to.equals('LibError');
expect(error.constructor.name).to.equals('LibError');
expect(error.stack).to.be.a('string').and.to.include(err.stack);
expect(error.originStack).to.be.a('string').and.to.equals(`\n${err.stack}`);
expect(error.toString()).to.be.a('string').and.to.include(error.stack);
expect(error.toString()).to.be.a('string').and.to.include(err.stack);

err = new Error();
err.code = 'ORIGIN_ERROR_CODE'
Expand All @@ -140,7 +146,9 @@ describe('#LibError', function() {
expect(error.message).to.equals('ORIGIN_ERROR_CODE');
expect(error.name).to.equals('LibError');
expect(error.constructor.name).to.equals('LibError');
expect(error.stack).to.be.a('string').and.to.include(err.stack);
expect(error.originStack).to.be.a('string').and.to.equals(`\n${err.stack}`);
expect(error.toString()).to.be.a('string').and.to.include(error.stack);
expect(error.toString()).to.be.a('string').and.to.include(err.stack);
});
});

Expand Down Expand Up @@ -236,7 +244,7 @@ describe('#LibError', function() {
expect(error.constructor.name).to.equals('LibError');
});

it('should create a lib error with a stack trace including origin error\'s stack', function() {
it('should create a lib error with the origin error stack trace and to be included in toString format', function() {
const origin = new URIError('bad uri');
const error = new LibError({}, origin);

Expand All @@ -245,7 +253,9 @@ describe('#LibError', function() {
expect(error.message).to.equals('bad uri');
expect(error.name).to.equals('LibError');
expect(error.constructor.name).to.equals('LibError');
expect(error.stack).to.be.a('string').and.to.include(origin.stack);
expect(error.originStack).to.be.a('string').and.to.equals(`\n${origin.stack}`);
expect(error.toString()).to.be.a('string').and.to.include(error.stack);
expect(error.toString()).to.be.a('string').and.to.include(origin.stack);
});
});
});

0 comments on commit 5c8518a

Please sign in to comment.