move errors.js to ts

This commit is contained in:
Audric Ackermann 2022-03-30 10:23:16 +11:00
parent 79bf0c53ee
commit 6e8e8eaa9a
8 changed files with 77 additions and 197 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,6 +20,7 @@ import { MessageSender } from '.';
import { getMessageById } from '../../../ts/data/data';
import { getConversationController } from '../conversations';
import { ed25519Str } from '../onions/onionPath';
import { EmptySwarmError } from '../utils/errors';
const DEFAULT_CONNECTIONS = 1;
@ -179,7 +180,7 @@ export async function TEST_sendMessageToSnode(
throw e;
}
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);

View File

@ -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
View File

@ -2,13 +2,16 @@ import {} from 'styled-components/cssprop';
import { LocalizerType } from '../ts/types/Util';
import { LibsignalProtocol } from '../../libtextsecure/libsignal-protocol';
import { LibTextsecure } from '../libtextsecure';
import { Store } from 'redux';
import { ConversationCollection, ConversationModel } from './models/conversation';
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.
If you import anything in global.d.ts, the type system won't work correctly.