move errors.js to ts

pull/2242/head
Audric Ackermann 3 years ago
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);
});
};
})();

@ -51,7 +51,6 @@ export class OpenGroupMessageV2 {
sender, sender,
}); });
} }
public async sign(): Promise<OpenGroupMessageV2> { public async sign(): Promise<OpenGroupMessageV2> {
const ourKeyPair = await UserUtils.getIdentityKeyPair(); const ourKeyPair = await UserUtils.getIdentityKeyPair();
if (!ourKeyPair) { if (!ourKeyPair) {

@ -1,5 +1,6 @@
import { default as insecureNodeFetch } from 'node-fetch'; import { default as insecureNodeFetch } from 'node-fetch';
import pRetry from 'p-retry'; import pRetry from 'p-retry';
import { HTTPError, NotFoundError } from '../../utils/errors';
import { Snode } from '../../../data/data'; import { Snode } from '../../../data/data';
import { getStoragePubKey } from '../../types'; import { getStoragePubKey } from '../../types';
@ -75,7 +76,7 @@ async function lokiFetch({
const response = await insecureNodeFetch(url, fetchOptions); const response = await insecureNodeFetch(url, fetchOptions);
if (!response.ok) { if (!response.ok) {
throw new window.textsecure.HTTPError('Loki_rpc error', response); throw new HTTPError('Loki_rpc error', response);
} }
const result = await response.text(); const result = await response.text();
@ -85,7 +86,7 @@ async function lokiFetch({
}; };
} catch (e) { } catch (e) {
if (e.code === 'ENOTFOUND') { if (e.code === 'ENOTFOUND') {
throw new window.textsecure.NotFoundError('Failed to resolve address', e); throw new NotFoundError('Failed to resolve address', e);
} }
if (e.message === ERROR_421_HANDLED_RETRY_REQUEST) { if (e.message === ERROR_421_HANDLED_RETRY_REQUEST) {
throw new pRetry.AbortError(ERROR_421_HANDLED_RETRY_REQUEST); throw new pRetry.AbortError(ERROR_421_HANDLED_RETRY_REQUEST);

@ -20,6 +20,7 @@ import { MessageSender } from '.';
import { getMessageById } from '../../../ts/data/data'; import { getMessageById } from '../../../ts/data/data';
import { getConversationController } from '../conversations'; import { getConversationController } from '../conversations';
import { ed25519Str } from '../onions/onionPath'; import { ed25519Str } from '../onions/onionPath';
import { EmptySwarmError } from '../utils/errors';
const DEFAULT_CONNECTIONS = 1; const DEFAULT_CONNECTIONS = 1;
@ -179,7 +180,7 @@ export async function TEST_sendMessageToSnode(
throw e; throw e;
} }
if (!usedNodes || usedNodes.length === 0) { if (!usedNodes || usedNodes.length === 0) {
throw new window.textsecure.EmptySwarmError(pubKey, 'Ran out of swarm nodes to query'); throw new EmptySwarmError(pubKey, 'Ran out of swarm nodes to query');
} }
const conversation = getConversationController().get(pubKey); const conversation = getConversationController().get(pubKey);

@ -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);
}
}
}

5
ts/window.d.ts vendored

@ -2,13 +2,16 @@ import {} from 'styled-components/cssprop';
import { LocalizerType } from '../ts/types/Util'; import { LocalizerType } from '../ts/types/Util';
import { LibsignalProtocol } from '../../libtextsecure/libsignal-protocol'; import { LibsignalProtocol } from '../../libtextsecure/libsignal-protocol';
import { LibTextsecure } from '../libtextsecure';
import { Store } from 'redux'; import { Store } from 'redux';
import { ConversationCollection, ConversationModel } from './models/conversation'; import { ConversationCollection, ConversationModel } from './models/conversation';
import { ConversationType } from './state/ducks/conversations'; import { ConversationType } from './state/ducks/conversations';
export interface LibTextsecure {
messaging: boolean;
}
/* /*
We declare window stuff here instead of global.d.ts because we are importing other declarations. We declare window stuff here instead of global.d.ts because we are importing other declarations.
If you import anything in global.d.ts, the type system won't work correctly. If you import anything in global.d.ts, the type system won't work correctly.

Loading…
Cancel
Save