BackupError.mjs (1767B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * Error class with specific backup-related error causes. 7 * 8 * Can be serialized and deserialized across a worker boundary using 9 * the BasePromiseWorker and PromiseWorker machinery in this codebase. 10 * 11 * @see PromiseWorker.mjs 12 * @see PromiseWorker.sys.mjs 13 */ 14 export class BackupError extends Error { 15 name = "BackupError"; 16 17 /** 18 * @param {string} message 19 * Error message 20 * @param {number} cause 21 * Error cause code from backup-constants.mjs:ERRORS 22 */ 23 constructor(message, cause) { 24 super(message, { cause }); 25 } 26 /** 27 * @typedef {object} SerializedBackupError 28 * @property {'BackupError'} exn 29 * Exception name for PromiseWorker serialization 30 * @property {string} message 31 * Error message 32 * @property {number} cause 33 * Error cause code from backup-constants.mjs:ERRORS 34 * @property {string} stack 35 * Stack trace of the error 36 */ 37 38 /** 39 * Used by PromiseWorker.mjs from within a web worker in order to 40 * serialize this error for later reconstruction in the main process. 41 * 42 * @returns {SerializedBackupError} 43 * @see PromiseWorker.mjs 44 */ 45 toMsg() { 46 return { 47 exn: BackupError.name, 48 message: this.message, 49 cause: this.cause, 50 stack: this.stack, 51 }; 52 } 53 54 /** 55 * @param {SerializedBackupError} serialized 56 * Worker error serialized by PromiseWorker 57 * @returns {BackupError} 58 */ 59 static fromMsg(serialized) { 60 let error = new BackupError(serialized.message, serialized.cause); 61 error.stack = serialized.stack; 62 return error; 63 } 64 }