-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: build and nits #3
Conversation
added all contributors and a readme starter to have a decent place as a homepage for this repo. The readme follows the kibibit readme template. changed license date to 2020 instead of 2019
almost the same behavior and jest has some extra features. Also, this is more uniform with the testing tools we use with other repos so should be easier to understand when contributing between repos
Changed the name assignment to take the name from the constructor. This should make it so that whenever an error extends KbError, the name will be initialized by the super call. This will be simpler and make sure it not depends on people forgetting to do this.name = '...' :-) https://javascript.info/custom-errors#breadcrumb-2:~:text=Now%20custom%20errors%20are%20much%20shorter%2C,%3D%20...%22%20line%20in%20the%20constructor.
- Removed some type checks. For example: We're not actually testing the message 'type', so it can be something else than a string. for example, passing an array is totally acceptable as a message. - Added some extra checks for 'should be extendable'
locally, and as a github action
didn't add a github action for this one. maybe later we'll add this: https://github.com/wagoid/commitlint-github-action
also removed the contributors shields since it doesn't fit the format
e2ab524
to
335e691
Compare
@@ -5,6 +5,6 @@ module.exports = class kbError extends Error { | |||
*/ | |||
constructor(message) { | |||
super(message); | |||
this.name = 'kbError'; | |||
this.name = this.constructor.name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this approach is when things get uglified and you lose the name.
I have no problem with this change as we're currently not uglifying, but we need to keep this in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Point!
But right now, I think this doesn't support browser as-is since it doesn't support RequireJS (because of the module.export =
part).
Even so, What you're worried about is easily fixable imo.
- we can always just use it as you intended when requiring on client-side, setting the name in the extended class
- Most minification\uglification libraries have support for this kind of exclusions.
You can just not minify class names. That might be too project-wide but it's an option :-)module.exports = { entry: './kb-error.js', output: { filename: 'kb-error.min.js', path: path.resolve(__dirname, 'dist'), }, optimization: { minimize: true, minimizer: [ new TerserPlugin({ terserOptions: { mangle: { keep_classnames: true } } }) ], } };
But then again, using:
class FooError extends kbError {
constructor(message) {
super(message);
}
}
should work without modification (node.js for example)
and:
class FooError extends kbError {
constructor(message) {
super(message);
this.name = 'FooError';
}
}
should work fine in the browser with this implementation
@@ -5,6 +5,6 @@ module.exports = class kbError extends Error { | |||
*/ | |||
constructor(message) { | |||
super(message); | |||
this.name = 'kbError'; | |||
this.name = this.constructor.name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this approach is when things get uglified and you lose the name.
I have no problem with this change as we're currently not uglifying, but we need to keep this in mind.
Should release versions to npm once this commit is in (this will commit the github action as well)
If this pull request is too big, I can split it up into a couple of pull requests if you want.
TL;DR (I explained my changes in the description of each commit):