Skip to content

Commit

Permalink
Add support for the browser too! (#2)
Browse files Browse the repository at this point in the history
* Support the browser too!

* Update package.json

* Ignore coverals and fix travis

* this works a bit better

* Fix this

* change that

* check the global & fix tests

* uniform naming and allow setting of commit hash

* Don’t create the mapping on every write

* Create default handler logger at init
  • Loading branch information
0xdeafcafe authored Jul 9, 2019
1 parent 2b1e3c3 commit 08481c9
Show file tree
Hide file tree
Showing 7 changed files with 888 additions and 1,081 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
.travis.yml
12 changes: 3 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
language: node_js
script: npm run-script test-travis
node_js:
- "10"
script: yarn test-travis
after_script: cat ./coverage/lcov.info | coveralls

notifications:
email: false
slack:
rooms:
- secure: gVo/2b4TunNkEpaFefPwKeeYiUigbQWHBmEoAWUoOMHe/OL8hY6tWBDSHMBHfp2hJuGs1QqYJ5fJ1UihJw/0rQ4PxcmJK2dcTxmBdrNNji7k/8V8lPBYMRXBEPBm9te669IPyC2cPXeJ5RWkR7OCPoRNXp5Pbrwiucg4K/n38BSFSypuXb77boINZm9aNLa76wnVkXYU+tX0P1tq3HC7ozWSbCNI/EoLO/EySOlB5Q/4SRVZ2g3Yk2/DVj8rD+nG1MM2E1NUlC5CaTHZkeWBxuP3lkSslGJF53lVGOnh2IOvurEDl+4InXTxF8+fy1wyJpTov3Imt5o24L8cmXVRYNgHLM31avZ5P/cPsvuO6WwbM2TTEJDJRFZxmCRAU1l3qd/UI7RXvdihlv6S5bOZzpCS1VxXnbjd1GonjOoq2kNwnSGNDzX+VlOUSV/TggAPBNeEetofid0zMOFtoHjhFwl6/h6Awle7FzljlLZr6rAzsD5OVExAj52RYOwEXJMWJ6sXZPRd3gFl9Ogamec0Ttt7D+C5/RlHAzoPuDT32R744YVuUIBGpoVeiWaH7jiI5OJVYiGllBOKuSZfkNTwwMiJeY8onmT5+LuA7enRrzjWE9HqrfR7bAF3bX7tyj31p7sKWpd1g0o/m1KBKgIk8XNMztPz0TXI3NZSjJsqzGM=
on_success: always
on_failure: always
37 changes: 33 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@ const CuvvaError = require('./cuvva-error');

module.exports.CuvvaError = CuvvaError;

function createDefaultHandlerLogger() {
if (process && process.stderr && process.stderr.write)
return (level, message, _) => process.stderr.write(`${message}\n`);

if (global.window && global.window.console) {
const consoleLevelMap = {
debug: global.window.console.debug,
info: global.window.console.info,
warn: global.window.console.warn,
warning: global.window.console.warn,
error: global.window.console.error,
fatal: global.window.console.error,
};

return (level, message, _) => consoleLevelMap[level](message);
}

return (level, message, error) => { throw error };
}

const defaultHandlerLogger = createDefaultHandlerLogger();

const errorLevels = {
debug: 0,
info: 1,
Expand All @@ -13,7 +35,7 @@ const errorLevels = {
const config = {
handlers: {},
minLogLevel: errorLevels.info,
gitCommit: process.env.GIT_COMMIT || 'dev',
commitHash: process.env.GIT_COMMIT || 'dev',
service: 'unknown',
};

Expand All @@ -24,17 +46,17 @@ function defaultHandler(level, error) {
...error,
stack: showStack ? error.stack : void 0,
reasons: error.reasons && error.reasons.map(r => ({ ...r, stack: showStack ? r.stack : void 0 })),
}
};

const message = JSON.stringify({
_level: level,
_timestamp: new Date().toISOString(),
_commit_hash: config.gitCommit,
_commit_hash: config.commitHash,
_service: config.service,
...formattedError,
});

process.stderr.write(message + '\n');
defaultHandlerLogger(level, message, error);
}

function handleError(level, error) {
Expand Down Expand Up @@ -70,6 +92,10 @@ module.exports.setMinLogLevel = function (level) {
config.minLogLevel = errorLevels[level];
};

module.exports.setCommitHash = function (hash) {
config.commitHash = hash;
}

module.exports.setService = function (service) {
config.service = service;
};
Expand All @@ -78,11 +104,14 @@ function logger(level) {
return function (code, reasons, meta) {
if (code instanceof CuvvaError) {
handleError(level, code);

return code;
}

const error = new CuvvaError(code, reasons, meta);

handleError(level, error);

return error;
};
}
Expand Down
Loading

0 comments on commit 08481c9

Please sign in to comment.