-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new error handling strategy
Resolves #46
- Loading branch information
Showing
6 changed files
with
68 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import MetricService from '../../../src/shared/metric_service.js'; | ||
import { InvalidMetricTypeError } from '../../../src/shared/model/error.js'; | ||
|
||
describe('Metric Service', () => { | ||
it('should throw if trying to emit an non-Metric', async () => { | ||
await expect(async () => { | ||
await MetricService.emit('my-made-up-metric'); | ||
}).rejects.toThrow(/Invalid metric/); | ||
//TODO https://github.com/lfilho/sample-webextension/issues/46 | ||
}).rejects.toThrow(InvalidMetricTypeError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* @see | ||
* /docs/development/ARCHITECTURE.md#error-handling | ||
*/ | ||
|
||
// Fake translation function, here as an example only | ||
const t = (string) => string; | ||
|
||
// Not supposed to be used directly, hence why not exporting it | ||
class MyProjectError extends Error { | ||
constructor(message) { | ||
/* | ||
* By calling `super` without args here, | ||
* we force ourselves to create a unique message in each error subclass instead | ||
*/ | ||
super(); | ||
|
||
this.name = this.constructor.name; // subclass' name | ||
|
||
// The argument passed will instead be used as additional debugging message | ||
this.debugMessage = message; | ||
} | ||
} | ||
|
||
export class InvalidMetricTypeError extends MyProjectError { | ||
message = t('Invalid metric. Metric must be an instance of Metric'); | ||
} | ||
|
||
export class ErrorSendingMetric extends MyProjectError { | ||
message = t('Error while sending metric. Did you pay your internet bill?'); | ||
} | ||
|
||
export class MetricInitializationError extends MyProjectError { | ||
message = t('Metric constructor needs both a dimension and value for it'); | ||
} | ||
|
||
export class InvalidMetricDimensionError extends MyProjectError { | ||
message = t('Invalid metric dimension. See `Metric.dimensions` for values.'); | ||
} | ||
|
||
export class ListTypeError extends MyProjectError { | ||
message = t('List constructor needs a type. See `List.types` for values.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters