This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Improve Error Categorization and Formatting #86
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
737318a
start on errors
linuxwolf 6fb2f21
clean up some more errors
linuxwolf bf6bcff
more customized errors
linuxwolf 981ee1f
filling in the corners
linuxwolf 3c9ff78
get coverage back up
linuxwolf 857c116
more coverage
linuxwolf 486b8b3
addressing code review comments
linuxwolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -4,27 +4,47 @@ | |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
const REASONS = [ | ||
"LOCALDB_VERSION", | ||
"NOT_INITIALIZED", | ||
"INITIALIZED", | ||
"MISSING_APP_KEY", | ||
"WRONG_APP_KEY", | ||
"CRYPTO", | ||
"LOCKED", | ||
"INVALID_ITEM", | ||
"MISSING_ITEM", | ||
"OFFLINE", | ||
"AUTH", | ||
"NETWORK", | ||
"SYNC_LOCKED", | ||
"GENERIC_ERROR" | ||
]; | ||
|
||
/** | ||
* Errors specific to DataStore operations. | ||
* | ||
*/ | ||
class DataStoreError extends Error { | ||
/** | ||
* @constructs | ||
* Creates a new DataStoreError with the given message and reason. | ||
* | ||
* @param {string} message - The error message | ||
* @param {Symbol} [reason=DataStoreError.GENERIC_ERROR] - The reason for the error | ||
* @param {string} [reason=DataStoreError.GENERIC_ERROR] - The reason for the error | ||
* @param {string} [message] - The error message | ||
*/ | ||
constructor(message, reason) { | ||
if ("symbol" === typeof message) { | ||
reason = message; | ||
message = ""; | ||
constructor(reason, message) { | ||
if (-1 === REASONS.indexOf(reason)) { | ||
message = reason; | ||
reason = null; | ||
} | ||
if (!reason) { | ||
reason = DataStoreError.GENERIC_ERROR; | ||
} | ||
if (!message) { | ||
message = /^Symbol\((.*)\)$/.exec(reason.toString())[1]; | ||
message = reason; | ||
} else { | ||
message = `${reason}: ${message}`; | ||
} | ||
|
||
super(message); | ||
|
@@ -36,50 +56,71 @@ class DataStoreError extends Error { | |
} | ||
|
||
/** | ||
* The reason for this DataStoreError. | ||
* The reason name for this DataStoreError. | ||
* | ||
* @member {Symbol} | ||
* @member {string} | ||
*/ | ||
this.reason = reason; | ||
} | ||
} | ||
|
||
REASONS.forEach((r) => DataStoreError[r] = r); | ||
|
||
/** | ||
* When opening a local database, the actual database version does not match the expected version. | ||
* @member {string} DataStoreError.LOCALDB_VERSION | ||
*/ | ||
/** | ||
* The datastore is not yet initialized. | ||
* | ||
* @type Symbol | ||
* @member {string} DataStoreError.NOT_INITIALIZED | ||
*/ | ||
DataStoreError.NOT_INITIALIZED = Symbol("NOT_INITIALIZED"); | ||
/** | ||
* An attempt was made to initialize a datastore that is already initialized. | ||
* | ||
* @type Symbol | ||
* @member {string} DataStoreError.INITIALIZED | ||
*/ | ||
DataStoreError.INITIALIZED = Symbol("INITIALIZED"); | ||
/** | ||
* When opening the local database, the actual database version does not | ||
* match the expected version. | ||
* | ||
* @type Symbol | ||
* No master key was provided. | ||
* @member {string} DataStoreError.MISSING_APP_KEY | ||
*/ | ||
/** | ||
* The master key is not valid for the encrypted data. | ||
* @member {string} DataStoreError.WRONG_APP_KEY | ||
*/ | ||
DataStoreError.LOCALDB_VERSION = Symbol("LOCALDB_VERSION"); | ||
/** | ||
* An attempt was made to use a datastore that is still locked. | ||
* | ||
* @type Symbol | ||
* @member {string} DataStoreError.LOCKED | ||
*/ | ||
DataStoreError.LOCKED = Symbol("LOCKED"); | ||
/** | ||
* The item to be added/updated is invalid. | ||
* | ||
* @type Symbol | ||
* @member {string} DataStoreError.INVALID_ITEM | ||
*/ | ||
DataStoreError.INVALID_ITEM = Symbol("INVALID_ITEM"); | ||
/** | ||
* An otherwise unspecified error occured with the datastore. | ||
* | ||
* @type Symbol | ||
* The item to be updated does not exist. | ||
* @member {string} DataStoreError.MISSING_ITEM | ||
*/ | ||
/** | ||
* There was a cryptographic error. | ||
* @member {string} DataStoreError.CRYPTO | ||
*/ | ||
/** | ||
* An operation requires network connectivety, but there is none. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. connectivity* |
||
* @member {string} DataStoreError.OFFLINE | ||
*/ | ||
/** | ||
* An operation requires (remote) authentication before it can be performed. | ||
* @member {string} DataStoreError.AUTH | ||
*/ | ||
/** | ||
* An operation encountered a (generic) network error. | ||
* @member {string} DataStoreError.NETWORK | ||
*/ | ||
/** | ||
* An attempt was made to sync a datastore, but cannot be completed until unlocked. | ||
* @member {string} DataStoreError.SYNC_LOCKED | ||
*/ | ||
/** | ||
* An otherwise unspecified error occurred with the datastore. | ||
* @member {string} DataStoreError.GENERIC_ERROR | ||
*/ | ||
DataStoreError.GENERIC_ERROR = Symbol("GENERIC_ERROR"); | ||
|
||
module.exports = DataStoreError; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if we want to use
Array#includes()
versusindexOf() === -1
. On that note, I'm not sure if we have any list of minimum browser support that we are planning, or if we should run this library through something like Babel to make it ES5 compliant (and minify it, etc).Also, not sure if we should log any REASON misses. Like, if somebody typed
DataStoreError.GENERIC_ERRROR
instead ofDataStoreError.GENERIC_ERROR
.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.
I can take a look at
Array#includes()
, but I've been hesitant to use APIs that I haven't had personal experience with on other platforms ... notably iOS WebKit (which is the target forlockbox-ios
).As far as minifying ... prove to me the size is an actual problem to deal with here, and not something that the consumers are better equiped for.