Skip to content

Commit

Permalink
Implement base Exception class that will use the name of the class.
Browse files Browse the repository at this point in the history
When exception types inherit from `Error`, the *name* property sticks with the
string "Error", making logs less useful.

Issue #259.

␄
  • Loading branch information
Mike Castle committed Feb 24, 2024
1 parent bfb7044 commit 6c2fec3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
54 changes: 54 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,59 @@ window.NexusHoratio.base = (function base() {

NH.xunit.testing.testCases.push(EnsureTestCase);

/** Base exception that uses the name of the class. */
class Exception extends Error {

/** @type {string} */
get name() {
return this.constructor.name;
}

}

/* eslint-disable require-jsdoc */
class ExceptionTestCase extends NH.xunit.TestCase {

testBase() {
// Assemble/Act
const e = new Exception(this.id);

// Assert
this.assertEqual(e.name, 'Exception', 'name');
this.assertEqual(
e.toString(), 'Exception: ExceptionTestCase.testBase', 'toString'
);
this.assertTrue(e instanceof Exception, 'is exception');
this.assertTrue(e instanceof Error, 'is error');
this.assertFalse(e instanceof TypeError, 'is NOT type-error');
}

testInheritance() {
// Assemble
class TestException extends Exception {}
class DifferentException extends Exception {}

// Act
const te = new TestException('silly message');

// Assert
this.assertEqual(te.name, 'TestException', 'name');
this.assertEqual(
te.toString(), 'TestException: silly message', 'toString'
);
this.assertTrue(te instanceof TestException, 'is test-exception');
this.assertTrue(te instanceof Exception, 'is exception');
this.assertTrue(te instanceof Error, 'is error');
this.assertFalse(te instanceof TypeError, 'is NOT type-error');
this.assertFalse(te instanceof DifferentException,
'is NOT different-exception');
}

}
/* eslint-enable */

NH.xunit.testing.testCases.push(ExceptionTestCase);

/**
* Simple dispatcher (event bus).
*
Expand Down Expand Up @@ -2279,6 +2332,7 @@ window.NexusHoratio.base = (function base() {
NOT_FOUND: NOT_FOUND,
ONE_ITEM: ONE_ITEM,
ensure: ensure,
Exception: Exception,
Dispatcher: Dispatcher,
MessageQueue: MessageQueue,
issues: issues,
Expand Down
3 changes: 2 additions & 1 deletion lib/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

Pure [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) stuff. Nothing here should be [WEB API](https://developer.mozilla.org/en-US/docs/Web/API) aware, except `Logger`'s use of *console*.

## Exported properties (as of version 50)
## Exported properties (as of version 52)
* version - Bumped per release.
* NOT_FOUND - Constant (to make eslint's `no-magic-numbers` setting happy).
* ONE_ITEM - Constant useful for testing length of an array.
* ensure - Ensures appropriate versions of NexusHoratio libraries are loaded.
* Exception - Base exception that uses the name of the class.
* Dispatcher - Simple dispatcher (event bus).
* MessageQueue - A simple message system that will queue messages to be delivered.
* issues - NexusHoratio libraries and apps should log issues here.
Expand Down

0 comments on commit 6c2fec3

Please sign in to comment.