Some of the Firefox profile backup code is executed within a web worker, so errors thrown in the worker do not automatically maintain their full context when caught in the main process. This change creates a BackupError for throwing errors with causes specific to Firefox profile backup. The new error type is configured to work with the PromiseWorker machinery in the Firefox codebase in order to serialize and deserialize error details across the worker boundary. Differential Revision: https://phabricator.services.mozilla.com/D215920
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
/**
|
|
* Error class with specific backup-related error causes.
|
|
*
|
|
* Can be serialized and deserialized across a worker boundary using
|
|
* the BasePromiseWorker and PromiseWorker machinery in this codebase.
|
|
*
|
|
* @see PromiseWorker.mjs
|
|
* @see PromiseWorker.sys.mjs
|
|
*/
|
|
export class BackupError extends Error {
|
|
name = "BackupError";
|
|
|
|
/**
|
|
* @param {string} message
|
|
* Error message
|
|
* @param {number} cause
|
|
* Error cause code @see BackupConstants.ERRORS
|
|
*/
|
|
constructor(message, cause) {
|
|
super(message, { cause });
|
|
}
|
|
/**
|
|
* @typedef {object} SerializedBackupError
|
|
* @property {'BackupError'} exn
|
|
* Exception name for PromiseWorker serialization
|
|
* @property {string} message
|
|
* Error message
|
|
* @property {number} cause
|
|
* Error cause code @see BackupConstants.ERRORS
|
|
* @property {string} stack
|
|
* Stack trace of the error
|
|
*/
|
|
|
|
/**
|
|
* Used by PromiseWorker.mjs from within a web worker in order to
|
|
* serialize this error for later reconstruction in the main process.
|
|
*
|
|
* @returns {SerializedBackupError}
|
|
* @see PromiseWorker.mjs
|
|
*/
|
|
toMsg() {
|
|
return {
|
|
exn: BackupError.name,
|
|
message: this.message,
|
|
cause: this.cause,
|
|
stack: this.stack,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param {SerializedBackupError} serialized
|
|
* Worker error serialized by PromiseWorker
|
|
* @returns {BackupError}
|
|
*/
|
|
static fromMsg(serialized) {
|
|
let error = new BackupError(serialized.message, serialized.cause);
|
|
error.stack = serialized.stack;
|
|
return error;
|
|
}
|
|
}
|