Compare commits

...

5 Commits

Author SHA1 Message Date
Ethan Arrowood
a419762690 config changes 2022-11-01 13:49:48 -06:00
Ethan Arrowood
7759f36894 fix import 2022-11-01 13:19:47 -06:00
Ethan Arrowood
4fa050ea25 rename to error-utils and convert to jest 2022-11-01 13:17:26 -06:00
Ethan Arrowood
1e229b8bbb replace is-error with new package 2022-11-01 12:40:47 -06:00
Ethan Arrowood
caa3245fc8 add errors package 2022-10-31 16:23:03 -06:00
35 changed files with 947 additions and 29 deletions

View File

@@ -42,6 +42,7 @@
},
"dependencies": {
"@vercel/build-utils": "5.5.5",
"@vercel/error-utils": "1.0.0",
"@vercel/go": "2.2.13",
"@vercel/hydrogen": "0.0.26",
"@vercel/next": "3.2.6",

View File

@@ -70,7 +70,7 @@ import getPrebuiltJson from '../../util/deploy/get-prebuilt-json';
import { createGitMeta } from '../../util/create-git-meta';
import { isValidArchive } from '../../util/deploy/validate-archive-format';
import { parseEnv } from '../../util/parse-env';
import { errorToString, isErrnoException, isError } from '../../util/is-error';
import { errorToString, isErrnoException, isError } from '@vercel/error-utils';
import { pickOverrides } from '../../util/projects/project-settings';
export default async (client: Client): Promise<number> => {

View File

@@ -15,7 +15,7 @@ import readConfig from '../../util/config/read-config';
import readJSONFile from '../../util/read-json-file';
import { getPkgName, getCommandName } from '../../util/pkg-name';
import { CantParseJSONFile } from '../../util/errors-ts';
import { isErrnoException } from '../../util/is-error';
import { isErrnoException } from '@vercel/error-utils';
const COMMAND_CONFIG = {
dev: ['dev'],

View File

@@ -11,7 +11,7 @@ import promptBool from '../../util/input/prompt-bool';
import purchaseDomain from '../../util/domains/purchase-domain';
import stamp from '../../util/output/stamp';
import { getCommandName } from '../../util/pkg-name';
import { errorToString } from '../../util/is-error';
import { errorToString } from '@vercel/error-utils';
type Options = {};

View File

@@ -18,7 +18,7 @@ import {
buildDeltaString,
createEnvObject,
} from '../../util/env/diff-env-files';
import { isErrnoException } from '../../util/is-error';
import { isErrnoException } from '@vercel/error-utils';
const CONTENTS_PREFIX = '# Created by Vercel CLI\n';

View File

@@ -7,7 +7,7 @@ import handleError from '../../util/handle-error';
import logo from '../../util/output/logo';
import init from './init';
import { getPkgName } from '../../util/pkg-name';
import { isError } from '../../util/is-error';
import { isError } from '@vercel/error-utils';
const COMMAND_CONFIG = {
init: ['init'],

View File

@@ -13,7 +13,7 @@ import { getDeployment } from '../util/get-deployment';
import { Deployment } from '@vercel/client';
import { Build } from '../types';
import title from 'title';
import { isErrnoException } from '../util/is-error';
import { isErrnoException } from '@vercel/error-utils';
import { isAPIError } from '../util/errors-ts';
import { URL } from 'url';

View File

@@ -20,7 +20,7 @@ import { getLinkedProject } from '../util/projects/link';
import { ensureLink } from '../util/link/ensure-link';
import getScope from '../util/get-scope';
import { isAPIError } from '../util/errors-ts';
import { isErrnoException } from '../util/is-error';
import { isErrnoException } from '@vercel/error-utils';
const help = () => {
console.log(`
@@ -56,7 +56,7 @@ const help = () => {
${chalk.gray('')} List all deployments for the project ${chalk.dim(
'`my-app`'
)} in the team of the currently linked project
${chalk.cyan(`$ ${getPkgName()} ls my-app`)}
${chalk.gray('')} Filter deployments by metadata

View File

@@ -6,7 +6,7 @@ import getArgs from '../util/get-args';
import Client from '../util/client';
import { getCommandName, getPkgName } from '../util/pkg-name';
import { isAPIError } from '../util/errors-ts';
import { errorToString } from '../util/is-error';
import { errorToString } from '@vercel/error-utils';
const help = () => {
console.log(`

View File

@@ -11,7 +11,7 @@ import { getPkgName, getCommandName } from '../../util/pkg-name';
import Client from '../../util/client';
import createTeam from '../../util/teams/create-team';
import patchTeam from '../../util/teams/patch-team';
import { errorToString, isError } from '../../util/is-error';
import { errorToString, isError } from '@vercel/error-utils';
const validateSlugKeypress = (data: string, value: string) =>
// TODO: the `value` here should contain the current value + the keypress

View File

@@ -12,7 +12,7 @@ import { email as regexEmail } from '../../util/input/regexes';
import getTeams from '../../util/teams/get-teams';
import inviteUserToTeam from '../../util/teams/invite-user-to-team';
import { isAPIError } from '../../util/errors-ts';
import { errorToString, isError } from '../../util/is-error';
import { errorToString, isError } from '@vercel/error-utils';
const validateEmail = (data: string) =>
regexEmail.test(data.trim()) || data.length === 0;

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env node
import { isErrnoException, isError, errorToString } from './util/is-error';
import { isErrnoException, isError, errorToString } from '@vercel/error-utils';
try {
// Test to see if cwd has been deleted before

View File

@@ -16,7 +16,7 @@ import { VERCEL_DIR } from '../projects/link';
import { Output } from '../output';
import readJSONFile from '../read-json-file';
import { CantParseJSONFile } from '../errors-ts';
import { errorToString, isErrnoException, isError } from '../is-error';
import { errorToString, isErrnoException, isError } from '@vercel/error-utils';
import cmd from '../output/cmd';
import code from '../output/code';

View File

@@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
import { resolve } from 'path';
import Client from '../client';
import { Cert } from '../../types';
import { isErrnoException } from '../is-error';
import { isErrnoException } from '@vercel/error-utils';
import { isAPIError } from '../errors-ts';
export default async function createCertFromFile(

View File

@@ -2,7 +2,7 @@ import retry from 'async-retry';
import { Cert } from '../../types';
import Client from '../client';
import { isAPIError } from '../errors-ts';
import { isError } from '../is-error';
import { isError } from '@vercel/error-utils';
// When it's a configuration error we should retry because of the DNS propagation
// otherwise we bail to handle the error in the upper level

View File

@@ -23,7 +23,7 @@ import type {
} from '../types';
import { sharedPromise } from './promise';
import { APIError } from './errors-ts';
import { normalizeError } from './is-error';
import { normalizeError } from '@vercel/error-utils';
const isSAMLError = (v: any): v is SAMLError => {
return v && v.saml;

View File

@@ -10,7 +10,7 @@ import error from '../output/error';
import highlight from '../output/highlight';
import { VercelConfig } from '../dev/types';
import { AuthConfig, GlobalConfig } from '../../types';
import { isErrnoException, isError } from '../is-error';
import { isErrnoException, isError } from '@vercel/error-utils';
const VERCEL_DIR = getGlobalPathConfig();
const CONFIG_FILE_PATH = join(VERCEL_DIR, 'config.json');

View File

@@ -5,7 +5,7 @@ import git from 'git-last-commit';
import { exec } from 'child_process';
import { GitMetadata, Project } from '../types';
import { Output } from './output';
import { errorToString } from './is-error';
import { errorToString } from '@vercel/error-utils';
export async function createGitMeta(
directory: string,

View File

@@ -94,7 +94,7 @@ import {
isErrnoException,
isError,
isSpawnError,
} from '../is-error';
} from '@vercel/error-utils';
import isURL from './is-url';
import { pickOverrides } from '../projects/project-settings';
import { replaceLocalhost } from './parse-listen';

View File

@@ -1,4 +1,4 @@
import { isErrnoException } from '../is-error';
import { isErrnoException } from '@vercel/error-utils';
const knownErrorsCodes = new Set([
'PAYMENT_REQUIRED',

View File

@@ -5,7 +5,7 @@ import { NowError } from './now-error';
import code from './output/code';
import { getCommandName } from './pkg-name';
import chalk from 'chalk';
import { isError } from './is-error';
import { isError } from '@vercel/error-utils';
/**
* This error is thrown when there is an API error with a payload. The error

View File

@@ -10,7 +10,7 @@ import humanizePath from './humanize-path';
import readJSONFile from './read-json-file';
import { VercelConfig } from './dev/types';
import { Output } from './output';
import { isErrnoException } from './is-error';
import { isErrnoException } from '@vercel/error-utils';
let config: VercelConfig;

View File

@@ -7,7 +7,7 @@ import executeLogin from './login';
import Client from '../client';
import { LoginResult } from './types';
import { isAPIError } from '../errors-ts';
import { errorToString } from '../is-error';
import { errorToString } from '@vercel/error-utils';
export default async function doEmailLogin(
client: Client,

View File

@@ -1,6 +1,6 @@
import Client from '../client';
import { InvalidEmail, AccountNotFound, isAPIError } from '../errors-ts';
import { errorToString } from '../is-error';
import { errorToString } from '@vercel/error-utils';
import { LoginData } from './types';
export default async function login(

View File

@@ -2,7 +2,7 @@ import chalk from 'chalk';
import renderLink from './link';
import wait, { StopSpinner } from './wait';
import type { WritableTTY } from '../../types';
import { errorToString } from '../is-error';
import { errorToString } from '@vercel/error-utils';
const IS_TEST = process.env.NODE_ENV === 'test';

View File

@@ -18,7 +18,7 @@ import { prependEmoji, emoji, EmojiLabel } from '../emoji';
import { isDirectory } from '../config/global-path';
import { NowBuildError, getPlatformEnv } from '@vercel/build-utils';
import outputCode from '../output/code';
import { isErrnoException, isError } from '../is-error';
import { isErrnoException, isError } from '@vercel/error-utils';
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

View File

@@ -1,7 +1,7 @@
import Client from './client';
import getScope from './get-scope';
import getArgs from './get-args';
import { isError } from './is-error';
import { isError } from '@vercel/error-utils';
import type { Team, User } from '../types';
export default async function reportError(

View File

@@ -0,0 +1,6 @@
const fs = require('node:fs');
const path = require('node:path');
const outDir = path.join(__dirname, 'dist');
fs.rmSync(outDir, { recursive: true, force: true });

View File

@@ -0,0 +1,13 @@
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
};

View File

@@ -0,0 +1,25 @@
{
"name": "@vercel/error-utils",
"private": "true",
"version": "1.0.0",
"description": "A collection of error utilities for vercel/vercel",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/vercel/vercel.git",
"directory": "packages/error-utils"
},
"scripts": {
"build": "tsc",
"test": "jest --coverage --env node --verbose"
},
"author": "Ethan Arrowood <ethan.arrowood@vercel.com>",
"license": "MIT",
"devDependencies": {
"@types/jest": "29.2.1",
"@types/node": "16.11.7",
"jest": "29.2.2",
"typescript": "^4.8.4"
}
}

View File

@@ -25,7 +25,6 @@ export const isError = (error: unknown): error is Error => {
// Walk the prototype tree until we find a matching object.
while (error) {
if (Object.prototype.toString.call(error) === '[object Error]') return true;
// eslint-disable-next-line no-param-reassign -- TODO: Fix eslint error following @vercel/style-guide migration
error = Object.getPrototypeOf(error);
}

148
packages/error-utils/test/index.test.ts vendored Normal file
View File

@@ -0,0 +1,148 @@
import fs from 'node:fs';
import {
isObject,
isError,
isErrnoException,
isErrorLike,
normalizeError,
isSpawnError,
errorToString,
} from '../src';
const ARRAY: any[] = [];
const BIGINT = 1n;
const BOOLEAN = true;
const FUNCTION = () => {};
const NULL = null;
const NUMBER = 0;
const OBJECT = {};
const STRING = '';
const SYMBOL = Symbol('');
const UNDEFINED = undefined;
class CLASS {} // `CLASS` is a function and `new CLASS()` is an Object
test('isObject returns true for objects only', () => {
for (const item of [ARRAY, new CLASS(), OBJECT]) {
expect(isObject(item)).toBe(true);
}
for (const item of [
BIGINT,
BOOLEAN,
CLASS,
FUNCTION,
NULL,
NUMBER,
STRING,
SYMBOL,
UNDEFINED,
]) {
expect(isObject(item)).toBe(false);
}
});
test('isError returns true for Error instances only', () => {
for (const error of [
new Error(),
new EvalError(),
new RangeError(),
new ReferenceError(),
new SyntaxError(),
new TypeError(),
new URIError(),
]) {
expect(isError(error)).toBe(true);
}
for (const item of [
ARRAY,
BIGINT,
BOOLEAN,
CLASS,
new CLASS(),
FUNCTION,
NULL,
NUMBER,
OBJECT,
STRING,
SYMBOL,
UNDEFINED,
]) {
expect(isError(item)).toBe(false);
}
});
test('isError returns true for objects with a nested Error prototype', () => {
class Foo {}
const err = new Error();
Object.setPrototypeOf(err, Foo.prototype);
expect(isError(err)).toBe(true);
});
test('isErrnoException returns true for NodeJS.ErrnoException only', () => {
try {
fs.statSync('./i-definitely-do-not-exist');
fail();
} catch (err) {
expect(isErrnoException(err)).toBe(true);
}
});
test('isErrorLike returns true when object is like an error', () => {
expect(isErrorLike(new Error())).toBe(true);
expect(isErrorLike({ message: '' })).toBe(true);
expect(isErrorLike({})).toBe(false);
});
describe('errorToString', () => {
const message = 'message';
test('return `message` when first argument is an error', () => {
expect(errorToString(new Error(message))).toStrictEqual(message);
});
test('returns `message` when first argument is error like', () => {
expect(errorToString({ message })).toStrictEqual(message);
});
test('returns first argument when it is a string', () => {
expect(errorToString(message)).toStrictEqual(message);
});
test('returns second argument when first argument is not an error, error like, nor a string', () => {
expect(errorToString(null, message)).toStrictEqual(message);
});
test('returns default fallback message when first argument is not an error, error like, nor a string, and the second argument is not provided', () => {
expect(errorToString(null)).toStrictEqual('An unknown error has ocurred.');
});
});
describe('normalizeError', () => {
const message = 'message';
test('returns first argument if it is an error', () => {
expect(normalizeError(new Error(message))).toStrictEqual(
new Error(message)
);
});
test('returns a new error if argument is not error like', () => {
expect(normalizeError(message)).toStrictEqual(new Error(message));
});
test('returns a new error if argument is not error like', () => {
expect(normalizeError({ message })).toStrictEqual(new Error(message));
});
test('returns a new error with fallback message if argument is not error like nor a string.', () => {
expect(normalizeError(null)).toStrictEqual(
new Error('An unknown error has ocurred.')
);
});
test('returns an Error with the input object assigned to it', () => {
expect(normalizeError({ message, prop: 'value' })).toStrictEqual(
Object.assign(new Error(message), { prop: 'value' })
);
});
});
test('isSpawnError', () => {
const spawnError = new Error('spawn error');
Object.assign(spawnError, {
code: 'SPAWN_ERROR',
spawnargs: ['a', 'b', 'c'],
});
expect(isSpawnError(spawnError)).toBe(true);
expect(isSpawnError(new Error('not spawn error'))).toBe(false);
});

View File

@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"]
},
"include": ["*.test.ts"]
}

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"lib": ["ES2020"],
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./dist",
"types": ["node"],
"strict": true,
"sourceMap": true,
"target": "ES2020"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

699
yarn.lock

File diff suppressed because it is too large Load Diff