move errors.js to ts
parent
79bf0c53ee
commit
6e8e8eaa9a
@ -1,108 +0,0 @@
|
||||
/* global window */
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
(function() {
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
function inherit(Parent, Child) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
Child.prototype = Object.create(Parent.prototype, {
|
||||
constructor: {
|
||||
value: Child,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
function appendStack(newError, originalError) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
newError.stack += `\nOriginal stack:\n${originalError.stack}`;
|
||||
}
|
||||
|
||||
function ReplayableError(options = {}) {
|
||||
this.name = options.name || 'ReplayableError';
|
||||
this.message = options.message;
|
||||
|
||||
Error.call(this, options.message);
|
||||
|
||||
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
||||
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
}
|
||||
|
||||
this.functionCode = options.functionCode;
|
||||
}
|
||||
inherit(Error, ReplayableError);
|
||||
|
||||
function SendMessageNetworkError(number, jsonData, httpError) {
|
||||
this.number = number;
|
||||
this.code = httpError.code;
|
||||
|
||||
ReplayableError.call(this, {
|
||||
name: 'SendMessageNetworkError',
|
||||
message: httpError.message,
|
||||
});
|
||||
|
||||
appendStack(this, httpError);
|
||||
}
|
||||
inherit(ReplayableError, SendMessageNetworkError);
|
||||
|
||||
function EmptySwarmError(number, message) {
|
||||
// eslint-disable-next-line prefer-destructuring
|
||||
this.number = number.split('.')[0];
|
||||
|
||||
ReplayableError.call(this, {
|
||||
name: 'EmptySwarmError',
|
||||
message,
|
||||
});
|
||||
}
|
||||
inherit(ReplayableError, EmptySwarmError);
|
||||
|
||||
function NotFoundError(message, error) {
|
||||
this.name = 'NotFoundError';
|
||||
this.message = message;
|
||||
this.error = error;
|
||||
|
||||
Error.call(this, message);
|
||||
|
||||
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
||||
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
}
|
||||
|
||||
appendStack(this, error);
|
||||
}
|
||||
|
||||
function HTTPError(message, response) {
|
||||
this.name = 'HTTPError';
|
||||
this.message = `${response.status} Error: ${message}`;
|
||||
this.response = response;
|
||||
|
||||
Error.call(this, message);
|
||||
|
||||
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
||||
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
}
|
||||
}
|
||||
|
||||
function TimestampError(message) {
|
||||
this.name = 'TimeStampError';
|
||||
|
||||
ReplayableError.call(this, {
|
||||
name: 'TimestampError',
|
||||
message,
|
||||
});
|
||||
}
|
||||
inherit(ReplayableError, TimestampError);
|
||||
|
||||
window.textsecure.SendMessageNetworkError = SendMessageNetworkError;
|
||||
window.textsecure.ReplayableError = ReplayableError;
|
||||
window.textsecure.EmptySwarmError = EmptySwarmError;
|
||||
window.textsecure.HTTPError = HTTPError;
|
||||
window.textsecure.NotFoundError = NotFoundError;
|
||||
window.textsecure.TimestampError = TimestampError;
|
||||
})();
|
@ -1,13 +0,0 @@
|
||||
import { LibTextsecureCryptoInterface } from './crypto';
|
||||
|
||||
export interface LibTextsecure {
|
||||
messaging: boolean;
|
||||
crypto: LibTextsecureCryptoInterface;
|
||||
storage: any;
|
||||
SendMessageNetworkError: any;
|
||||
ReplayableError: any;
|
||||
EmptySwarmError: any;
|
||||
HTTPError: any;
|
||||
NotFoundError: any;
|
||||
TimestampError: any;
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
/* global window */
|
||||
|
||||
/* eslint-disable more/no-then */
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
(function() {
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
window.textsecure.createTaskWithTimeout = (task, id, options = {}) => {
|
||||
const timeout = options.timeout || 1000 * 60 * 3; // three minutes
|
||||
|
||||
const errorForStack = new Error('for stack');
|
||||
return () =>
|
||||
new Promise((resolve, reject) => {
|
||||
let complete = false;
|
||||
let timer = setTimeout(() => {
|
||||
if (!complete) {
|
||||
const message = `${id || ''} task did not complete in time. Calling stack: ${
|
||||
errorForStack.stack
|
||||
}`;
|
||||
|
||||
window.log.error(message);
|
||||
return reject(new Error(message));
|
||||
}
|
||||
|
||||
return null;
|
||||
}, timeout);
|
||||
const clearTimer = () => {
|
||||
try {
|
||||
const localTimer = timer;
|
||||
if (localTimer) {
|
||||
timer = null;
|
||||
clearTimeout(localTimer);
|
||||
}
|
||||
} catch (error) {
|
||||
window.log.error(
|
||||
id || '',
|
||||
'task ran into problem canceling timer. Calling stack:',
|
||||
errorForStack.stack
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const success = result => {
|
||||
clearTimer();
|
||||
complete = true;
|
||||
return resolve(result);
|
||||
};
|
||||
const failure = error => {
|
||||
clearTimer();
|
||||
complete = true;
|
||||
return reject(error);
|
||||
};
|
||||
|
||||
let promise;
|
||||
try {
|
||||
promise = task();
|
||||
} catch (error) {
|
||||
clearTimer();
|
||||
throw error;
|
||||
}
|
||||
if (!promise || !promise.then) {
|
||||
clearTimer();
|
||||
complete = true;
|
||||
return resolve(promise);
|
||||
}
|
||||
|
||||
return promise.then(success, failure);
|
||||
});
|
||||
};
|
||||
})();
|
@ -0,0 +1,68 @@
|
||||
import { Response } from 'node-fetch';
|
||||
|
||||
export class EmptySwarmError extends Error {
|
||||
public error: any;
|
||||
public pubkey: string;
|
||||
constructor(pubkey: string, message: string) {
|
||||
// 'Error' breaks prototype chain here
|
||||
super(message);
|
||||
this.pubkey = pubkey.split('.')[0];
|
||||
this.name = 'EmptySwarmError';
|
||||
|
||||
// restore prototype chain
|
||||
const actualProto = new.target.prototype;
|
||||
|
||||
if (Object.setPrototypeOf) {
|
||||
Object.setPrototypeOf(this, actualProto);
|
||||
}
|
||||
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
||||
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError extends Error {
|
||||
public error: any;
|
||||
constructor(message: string, error: any) {
|
||||
// 'Error' breaks prototype chain here
|
||||
super(message);
|
||||
this.error = error;
|
||||
this.name = 'NotFoundError';
|
||||
|
||||
// restore prototype chain
|
||||
const actualProto = new.target.prototype;
|
||||
|
||||
if (Object.setPrototypeOf) {
|
||||
Object.setPrototypeOf(this, actualProto);
|
||||
}
|
||||
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
||||
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class HTTPError extends Error {
|
||||
public response: Response;
|
||||
constructor(message: string, response: Response) {
|
||||
// 'Error' breaks prototype chain here
|
||||
super(`${response.status} Error: ${message}`);
|
||||
this.response = response;
|
||||
this.name = 'HTTPError';
|
||||
|
||||
// restore prototype chain
|
||||
const actualProto = new.target.prototype;
|
||||
|
||||
if (Object.setPrototypeOf) {
|
||||
Object.setPrototypeOf(this, actualProto);
|
||||
}
|
||||
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
||||
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue