Skip to content

Commit e2eeff1

Browse files
committed
Move error utilities to @httptoolkit/util too
1 parent 7371ec2 commit e2eeff1

File tree

12 files changed

+23
-77
lines changed

12 files changed

+23
-77
lines changed

src/api/rest-api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import type {
1010
RouteParameters
1111
} from 'express-serve-static-core';
1212
import type { ParsedQs } from 'qs';
13+
import { ErrorLike, StatusError } from '@httptoolkit/util';
1314

14-
import { ErrorLike, StatusError } from '../util/error';
1515
import { logError } from '../error-tracking';
1616
import { ApiModel } from './api-model';
1717
import * as Client from '../client/client-types';
@@ -195,8 +195,8 @@ function handleErrors<
195195
// Use default error handler if response started (kills the connection)
196196
if (res.headersSent) return next(error)
197197
else {
198-
const status = (error.status && error.status >= 400 && error.status < 600)
199-
? error.status
198+
const status = (error.statusCode && error.statusCode >= 400 && error.statusCode < 600)
199+
? error.statusCode
200200
: 500;
201201

202202
res.status(status).send({

src/browsers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
22
import { promisify } from 'util';
33

4-
import { delay } from '@httptoolkit/util';
4+
import { delay, isErrorLike } from '@httptoolkit/util';
55
import getBrowserLauncherCb from '@httptoolkit/browser-launcher';
66
import {
77
LaunchOptions,
@@ -12,7 +12,6 @@ import {
1212
} from '@httptoolkit/browser-launcher';
1313

1414
import { logError } from './error-tracking';
15-
import { isErrorLike } from './util/error';
1615
import { readFile, deleteFile } from './util/fs';
1716

1817
const getBrowserLauncher = promisify(getBrowserLauncherCb);

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as fs from 'fs';
33
import envPaths from 'env-paths';
44
import { Mutex } from 'async-mutex';
55

6-
import { delay } from '@httptoolkit/util';
6+
import { delay, isErrorLike } from '@httptoolkit/util';
77
import {
88
PluggableAdmin,
99
generateCACertificate
@@ -22,7 +22,6 @@ import { checkBrowserConfig } from './browsers';
2222
import { logError } from './error-tracking';
2323
import { IS_PROD_BUILD, MOCKTTP_ALLOWED_ORIGINS } from './constants';
2424

25-
import { isErrorLike } from './util/error';
2625
import { readFile, checkAccess, writeFile, ensureDirectoryExists } from './util/fs';
2726

2827
import { registerShutdownHandler, shutdown } from './shutdown';

src/interceptors/android/adb-commands.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import * as stream from 'stream';
22
import * as path from 'path';
33

44
import adb, * as Adb from '@devicefarmer/adbkit';
5-
import { delay } from '@httptoolkit/util';
5+
import { delay, isErrorLike } from '@httptoolkit/util';
66

77
import { logError } from '../../error-tracking';
8-
import { isErrorLike } from '../../util/error';
98
import { waitUntil } from '../../util/promise';
109
import { getCertificateFingerprint, parseCert } from '../../certificates';
1110
import { streamToBuffer } from '../../util/stream';

src/interceptors/electron.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import _ from 'lodash';
22
import { spawn } from 'child_process';
33
import * as path from 'path';
44

5-
import { delay } from '@httptoolkit/util';
5+
import { delay, ErrorLike, isErrorLike } from '@httptoolkit/util';
66
import { getPortPromise as getPort } from 'portfinder';
77
import { generateSPKIFingerprint } from 'mockttp';
88
import ChromeRemoteInterface = require('chrome-remote-interface');
99

1010
import { Interceptor } from '.';
1111

1212
import { HtkConfig } from '../config';
13-
import { ErrorLike, isErrorLike } from '../util/error';
1413
import { canAccess, readFile } from '../util/fs';
1514
import { windowsClose } from '../util/process-management';
1615
import { getInheritableCurrentEnv, getTerminalEnvVars, OVERRIDES_DIR } from './terminal/terminal-env-overrides';

src/interceptors/fresh-firefox.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import _ from 'lodash';
22
import * as path from 'path';
33
import { SpawnOptions } from 'child_process';
44

5-
import { delay } from '@httptoolkit/util';
5+
import { delay, isErrorLike } from '@httptoolkit/util';
66

77
import { APP_ROOT } from '../constants';
88
import { HtkConfig } from '../config';
99
import { logError } from '../error-tracking';
1010

11-
import { isErrorLike } from '../util/error';
1211
import { isSnap, getSnapConfigPath } from '../util/snap';
1312

1413
import { launchBrowser, BrowserInstance, getBrowserDetails } from '../browsers';

src/interceptors/frida/frida-integration.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as FridaJs from 'frida-js';
2+
import { CustomError } from '@httptoolkit/util';
23

34
import { getReachableInterfaces } from '../../util/network';
45
import { buildIpTestScript } from './frida-scripts';
5-
import { ErrorWithCode } from '../../util/error';
66

77
/**
88
* Terminology:
@@ -46,7 +46,11 @@ export async function testAndSelectProxyAddress(
4646
ips.push(...options.extraAddresses);
4747
}
4848

49-
if (ips.length === 0) throw new ErrorWithCode('unreachable-proxy', "Couldn't detect proxy external IP");
49+
if (ips.length === 0) {
50+
throw new CustomError("Couldn't detect proxy external IP", {
51+
code: 'unreachable-proxy'
52+
});
53+
}
5054

5155
const ipTestScript = await buildIpTestScript(ips, proxyPort);
5256

@@ -57,13 +61,11 @@ export async function testAndSelectProxyAddress(
5761
if (message.payload.type === 'connected') {
5862
resolve(message.payload.ip as string);
5963
} else if (message.payload.type === 'connection-failed') {
60-
reject(new ErrorWithCode('unreachable-proxy',
61-
`Could not connect to proxy on port ${proxyPort} at ${
62-
ips.length > 1
63-
? `any of: ${ips.join(', ')}`
64-
: ips[0]
65-
}`
66-
));
64+
reject(new CustomError(`Could not connect to proxy on port ${proxyPort} at ${
65+
ips.length > 1
66+
? `any of: ${ips.join(', ')}`
67+
: ips[0]
68+
}`, { code: 'unreachable-proxy' }));
6769
} else {
6870
reject(new Error(`Unexpected message type: ${message.payload.type}`));
6971
}

src/interceptors/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import _ from 'lodash';
2+
import { ErrorLike } from '@httptoolkit/util';
23

34
import { HtkConfig } from '../config';
45
import { addShutdownHandler } from '../shutdown';
5-
import { ErrorLike } from '../util/error';
66

77
import { FreshFirefox, FreshFirefoxDeveloper, FreshFirefoxNightly } from './fresh-firefox';
88
import {

src/interceptors/jvm.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import * as path from 'path';
33

4-
import { delay } from '@httptoolkit/util';
4+
import { delay, ErrorLike } from '@httptoolkit/util';
55

66
import { Interceptor } from '.';
77

@@ -10,7 +10,6 @@ import { spawnToResult, waitForExit } from '../util/process-management';
1010
import { OVERRIDE_JAVA_AGENT } from './terminal/terminal-env-overrides';
1111
import { logError } from '../error-tracking';
1212
import { commandExists, canAccess } from '../util/fs';
13-
import { ErrorLike } from '../util/error';
1413

1514
type JvmTarget = { pid: string, name: string, interceptedByProxy: number | undefined };
1615

src/interceptors/terminal/fresh-terminal-interceptor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import _ from 'lodash';
22
import { spawn, ChildProcess, SpawnOptions } from 'child_process';
33
import * as GSettings from 'node-gsettings-wrapper';
44

5+
import { isErrorLike } from '@httptoolkit/util';
56
import { findExecutableById } from '@httptoolkit/osx-find-executable';
67

78
import { Interceptor } from '..';
89
import { HtkConfig } from '../../config';
910
import { logError, addBreadcrumb } from '../../error-tracking';
10-
import { isErrorLike } from '../../util/error';
1111
import { canAccess, commandExists, getRealPath, resolveCommandPath } from '../../util/fs';
1212
import { spawnToResult } from '../../util/process-management';
1313

0 commit comments

Comments
 (0)