Skip to content

Commit

Permalink
refactor: remove DetailledError, not supported by Listr renderer (#…
Browse files Browse the repository at this point in the history
…8525)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
charlypoly and github-actions[bot] committed Nov 1, 2022
1 parent d821961 commit 63dc8f2
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 105 deletions.
@@ -0,0 +1,5 @@
---
"@graphql-codegen/flutter-freezed": patch
---
dependencies updates:
- Updated dependency [`@graphql-codegen/visitor-plugin-common@2.13.0` ↗︎](https://www.npmjs.com/package/@graphql-codegen/visitor-plugin-common/v/2.13.0) (from `2.12.0`, in `dependencies`)
9 changes: 9 additions & 0 deletions .changeset/great-mails-yawn.md
@@ -0,0 +1,9 @@
---
'@graphql-codegen/cli': patch
'@graphql-codegen/core': patch
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/near-operation-file-preset': patch
'@graphql-codegen/plugin-helpers': patch
---

remove `DetailledError`, not supported by Listr renderer
15 changes: 6 additions & 9 deletions packages/graphql-codegen-cli/src/cli.ts
Expand Up @@ -2,7 +2,6 @@ import { generate } from './generate-and-save.js';
import { init } from './init/index.js';
import { createContext } from './config.js';
import { lifecycleHooks } from './hooks.js';
import { DetailedError } from '@graphql-codegen/plugin-helpers';

export async function runCli(cmd: string): Promise<number> {
await ensureGraphQlPackage();
Expand Down Expand Up @@ -33,14 +32,12 @@ export async function ensureGraphQlPackage() {
try {
await import('graphql');
} catch (e) {
throw new DetailedError(
`Unable to load "graphql" package. Please make sure to install "graphql" as a dependency!`,
`
To install "graphql", run:
yarn add graphql
Or, with NPM:
npm install --save graphql
`
throw new Error(
`Unable to load "graphql" package. Please make sure to install "graphql" as a dependency! \n
To install "graphql", run:
yarn add graphql
Or, with NPM:
npm install --save graphql`
);
}
}
28 changes: 9 additions & 19 deletions packages/graphql-codegen-cli/src/codegen.ts
@@ -1,12 +1,10 @@
import {
DetailedError,
Types,
CodegenPlugin,
normalizeOutputParam,
normalizeInstanceOrArray,
normalizeConfig,
getCachedDocumentNodeFromSchema,
isDetailedError,
} from '@graphql-codegen/plugin-helpers';
import { codegen } from '@graphql-codegen/core';

Expand Down Expand Up @@ -51,7 +49,7 @@ const makeDefaultLoader = (from: string) => {
};
};

type Ctx = { errors: DetailedError[] | Error[] };
type Ctx = { errors: Error[] };

function createCache(): <T>(namespace: string, key: string, factory: () => Promise<T>) => Promise<T> {
const cache = new Map<string, Promise<unknown>>();
Expand Down Expand Up @@ -123,9 +121,8 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
const generateKeys = Object.keys(config.generates || {});

if (generateKeys.length === 0) {
throw new DetailedError(
'Invalid Codegen Configuration!',
`
throw new Error(
`Invalid Codegen Configuration! \n
Please make sure that your codegen config file contains the "generates" field, with a specification for the plugins you need.
It should looks like that:
Expand All @@ -136,18 +133,16 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
my-file.ts:
- plugin1
- plugin2
- plugin3
`
- plugin3`
);
}

for (const filename of generateKeys) {
const output = (generates[filename] = normalizeOutputParam(config.generates[filename]));

if (!output.preset && (!output.plugins || output.plugins.length === 0)) {
throw new DetailedError(
'Invalid Codegen Configuration!',
`
throw new Error(
`Invalid Codegen Configuration! \n
Please make sure that your codegen config file has defined plugins list for output "${filename}".
It should looks like that:
Expand All @@ -173,9 +168,8 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
(generates[filename].schema as unknown as any[]).length === 0)
)
) {
throw new DetailedError(
'Invalid Codegen Configuration!',
`
throw new Error(
`Invalid Codegen Configuration! \n
Please make sure that your codegen config file contains either the "schema" field
or every generated file has its own "schema" field.
Expand Down Expand Up @@ -419,11 +413,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
}

if (executedContext.errors.length > 0) {
const errors = executedContext.errors.map(subErr =>
isDetailedError(subErr)
? `${subErr.message} for "${subErr.source}"${subErr.details}`
: subErr.message || subErr.toString()
);
const errors = executedContext.errors.map(subErr => subErr.message || subErr.toString());
const newErr = new AggregateError(executedContext.errors, `${errors.join('\n\n')}`);
// Best-effort to all stack traces for debugging
newErr.stack = `${newErr.stack}\n\n${executedContext.errors.map(subErr => subErr.stack).join('\n\n')}`;
Expand Down
14 changes: 5 additions & 9 deletions packages/graphql-codegen-cli/src/config.ts
Expand Up @@ -2,7 +2,6 @@ import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
import { TypeScriptLoader } from 'cosmiconfig-typescript-loader';
import { resolve } from 'path';
import {
DetailedError,
Types,
Profiler,
createProfiler,
Expand Down Expand Up @@ -156,8 +155,7 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte

if (!result) {
if (configFilePath) {
throw new DetailedError(
`Config ${configFilePath} does not exist`,
throw new Error(
`
Config ${configFilePath} does not exist.
Expand All @@ -168,18 +166,16 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte
);
}

throw new DetailedError(
`Unable to find Codegen config file!`,
`
throw new Error(
`Unable to find Codegen config file! \n
Please make sure that you have a configuration file under the current directory!
`
);
}

if (result.isEmpty) {
throw new DetailedError(
`Found Codegen config file but it was empty!`,
`
throw new Error(
`Found Codegen config file but it was empty! \n
Please make sure that you have a valid configuration file under the current directory!
`
);
Expand Down
8 changes: 3 additions & 5 deletions packages/graphql-codegen-cli/src/plugins.ts
@@ -1,4 +1,4 @@
import { DetailedError, Types, CodegenPlugin } from '@graphql-codegen/plugin-helpers';
import { Types, CodegenPlugin } from '@graphql-codegen/plugin-helpers';
import { resolve } from 'path';

export async function getPluginByName(
Expand All @@ -23,8 +23,7 @@ export async function getPluginByName(
return await pluginLoader(moduleName);
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ERR_MODULE_NOT_FOUND') {
throw new DetailedError(
`Unable to load template plugin matching ${name}`,
throw new Error(
`
Unable to load template plugin matching '${name}'.
Reason:
Expand All @@ -43,8 +42,7 @@ export async function getPluginByName(
)
.join('');

throw new DetailedError(
`Unable to find template plugin matching ${name}`,
throw new Error(
`
Unable to find template plugin matching '${name}'
Install one of the following packages:
Expand Down
4 changes: 1 addition & 3 deletions packages/graphql-codegen-cli/src/utils/cli-error.ts
@@ -1,6 +1,4 @@
import { DetailedError } from '@graphql-codegen/plugin-helpers';

type CompositeError = Error | DetailedError;
type CompositeError = Error;
type ListrError = Error & { errors: CompositeError[] };
export function isListrError(err: Error & { name?: unknown; errors?: unknown }): err is ListrError {
return err.name === 'ListrError' && Array.isArray(err.errors) && err.errors.length > 0;
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql-codegen-cli/tests/codegen.spec.ts
Expand Up @@ -112,7 +112,7 @@ describe('Codegen Executor', () => {
throw new Error(SHOULD_NOT_THROW_STRING);
} catch (e) {
expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING);
expect(e.message).toBe('Invalid Codegen Configuration!');
expect(e.message).toMatch('Invalid Codegen Configuration!');
}
});

Expand Down Expand Up @@ -143,7 +143,7 @@ describe('Codegen Executor', () => {
expect(output.length).toBe(1);
} catch (e) {
expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING);
expect(e.message).not.toBe('Invalid Codegen Configuration!');
expect(e.message).not.toMatch('Invalid Codegen Configuration!');
}
});

Expand All @@ -159,7 +159,7 @@ describe('Codegen Executor', () => {

throw new Error(SHOULD_NOT_THROW_STRING);
} catch (e) {
expect(e.message).toBe('Invalid Codegen Configuration!');
expect(e.message).toMatch('Invalid Codegen Configuration!');
expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING);
}
});
Expand All @@ -177,7 +177,7 @@ describe('Codegen Executor', () => {
throw new Error(SHOULD_NOT_THROW_STRING);
} catch (e) {
expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING);
expect(e.message).toBe('Invalid Codegen Configuration!');
expect(e.message).toMatch('Invalid Codegen Configuration!');
}
});

Expand Down
7 changes: 2 additions & 5 deletions packages/graphql-codegen-core/src/codegen.ts
@@ -1,5 +1,4 @@
import {
DetailedError,
Types,
isComplexPluginOutput,
federationSpec,
Expand Down Expand Up @@ -283,10 +282,8 @@ function validateDuplicateDocuments(files: Types.DocumentFile[]) {
.join('');

const definitionKindName = kind.replace('Definition', '').toLowerCase();
throw new DetailedError(
`Not all ${definitionKindName}s have an unique name: ${duplicated.join(', ')}`,
`
Not all ${definitionKindName}s have an unique name
throw new Error(
`Not all ${definitionKindName}s have an unique name: ${duplicated.join(', ')}: \n
${list}
`
);
Expand Down
12 changes: 5 additions & 7 deletions packages/graphql-codegen-core/src/execute-plugin.ts
@@ -1,4 +1,4 @@
import { DetailedError, Types, CodegenPlugin, Profiler, createNoopProfiler } from '@graphql-codegen/plugin-helpers';
import { Types, CodegenPlugin, Profiler, createNoopProfiler } from '@graphql-codegen/plugin-helpers';
import { DocumentNode, GraphQLSchema, buildASTSchema } from 'graphql';

export interface ExecutePluginOptions {
Expand All @@ -17,9 +17,8 @@ export interface ExecutePluginOptions {

export async function executePlugin(options: ExecutePluginOptions, plugin: CodegenPlugin): Promise<Types.PluginOutput> {
if (!plugin || !plugin.plugin || typeof plugin.plugin !== 'function') {
throw new DetailedError(
`Invalid Custom Plugin "${options.name}"`,
`
throw new Error(
`Invalid Custom Plugin "${options.name}" \n
Plugin ${options.name} does not export a valid JS object with "plugin" function.
Make sure your custom plugin is written in the following form:
Expand Down Expand Up @@ -54,9 +53,8 @@ export async function executePlugin(options: ExecutePluginOptions, plugin: Codeg
`Plugin ${options.name} validate`
);
} catch (e) {
throw new DetailedError(
`Plugin "${options.name}" validation failed:`,
`
throw new Error(
`Plugin "${options.name}" validation failed: \n
${e.message}
`
);
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/dart/flutter-freezed/package.json
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"@graphql-codegen/plugin-helpers": "^2.6.0",
"@graphql-codegen/schema-ast": "^2.5.0",
"@graphql-codegen/visitor-plugin-common": "2.12.0",
"@graphql-codegen/visitor-plugin-common": "2.13.0",
"auto-bind": "~4.0.0",
"tslib": "~2.4.0",
"change-case-all": "1.0.14"
Expand Down
13 changes: 6 additions & 7 deletions packages/plugins/other/visitor-plugin-common/src/enum-values.ts
@@ -1,6 +1,5 @@
import { EnumValuesMap, ParsedEnumValuesMap } from './types.js';
import { GraphQLSchema, isEnumType, GraphQLEnumType } from 'graphql';
import { DetailedError } from '@graphql-codegen/plugin-helpers';
import { parseMapper } from './mappers.js';

function escapeString(str: string) {
Expand Down Expand Up @@ -37,9 +36,9 @@ export function parseEnumValues({
const invalidMappings = Object.keys(mapOrStr).filter(gqlName => !allEnums.includes(gqlName));

if (invalidMappings.length > 0) {
throw new DetailedError(
`Invalid 'enumValues' mapping!`,
`The following types does not exist in your GraphQL schema: ${invalidMappings.join(', ')}`
throw new Error(
`Invalid 'enumValues' mapping! \n
The following types does not exist in your GraphQL schema: ${invalidMappings.join(', ')}`
);
}

Expand Down Expand Up @@ -74,9 +73,9 @@ export function parseEnumValues({
},
};
}
throw new DetailedError(
`Invalid "enumValues" configuration`,
`Enum "${gqlIdentifier}": expected string or object (with enum values mapping)`
throw new Error(
`Invalid "enumValues" configuration \n
Enum "${gqlIdentifier}": expected string or object (with enum values mapping)`
);
}, {} as ParsedEnumValuesMap);
}
Expand Down
@@ -1,4 +1,4 @@
import { isUsingTypes, Types, DetailedError } from '@graphql-codegen/plugin-helpers';
import { isUsingTypes, Types } from '@graphql-codegen/plugin-helpers';
import {
generateImportStatement,
ImportSource,
Expand Down Expand Up @@ -88,13 +88,10 @@ export function resolveDocumentImports<T>(
externalFragments,
};
} catch (e) {
throw new DetailedError(
`Unable to validate GraphQL document!`,
`
File ${documentFile.location} caused error:
${e.message || e.toString()}
`,
documentFile.location
throw new Error(
`Unable to validate GraphQL document! \n
File ${documentFile.location} caused error:
${e.message || e.toString()}`
);
}
});
Expand Down
11 changes: 0 additions & 11 deletions packages/utils/plugins-helpers/src/errors.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/utils/plugins-helpers/src/index.ts
Expand Up @@ -3,7 +3,6 @@ export * from './types.js';
export * from './utils.js';
export * from './helpers.js';
export * from './federation.js';
export * from './errors.js';
export * from './getCachedDocumentNodeFromSchema.js';
export * from './oldVisit.js';
export * from './profiler.js';
Expand Down
13 changes: 13 additions & 0 deletions patches/listr2+4.0.5.patch
@@ -0,0 +1,13 @@
diff --git a/node_modules/listr2/dist/index.cjs b/node_modules/listr2/dist/index.cjs
index 97af302..8715bd9 100644
--- a/node_modules/listr2/dist/index.cjs
+++ b/node_modules/listr2/dist/index.cjs
@@ -406,7 +406,7 @@ var _DefaultRenderer = class {
}
str = `${icon} ${str}`;
let parsedStr;
- let columns = process.stdout.columns ? process.stdout.columns : 80;
+ let columns = process.stdout.columns ? process.stdout.columns : process.platform === 'win32' ? 1000 : 80;
columns = columns - level * this.options.indentation - 2;
switch (this.options.formatOutput) {
case "truncate":

0 comments on commit 63dc8f2

Please sign in to comment.