Skip to content

Commit f45dcfa

Browse files
committed
fix: resolve TypeScript errors
1 parent f59f532 commit f45dcfa

File tree

11 files changed

+75
-37
lines changed

11 files changed

+75
-37
lines changed

packages/codemirror-graphql/src/utils/jsonParse.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,18 @@ function expect(str: string) {
155155
throw syntaxError(`Expected ${str} but found ${found}.`);
156156
}
157157

158+
type SyntaxErrorPosition = { start: number; end: number };
159+
160+
export class JSONSyntaxError extends Error {
161+
readonly position: SyntaxErrorPosition;
162+
constructor(message: string, position: SyntaxErrorPosition) {
163+
super(message);
164+
this.position = position;
165+
}
166+
}
167+
158168
function syntaxError(message: string) {
159-
return { message, start, end };
169+
return new JSONSyntaxError(message, { start, end });
160170
}
161171

162172
function skip(k: string) {

packages/codemirror-graphql/src/variables/lint.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from 'graphql';
1919

2020
import jsonParse, {
21+
JSONSyntaxError,
2122
ParseArrayOutput,
2223
ParseObjectOutput,
2324
ParseValueOutput,
@@ -57,11 +58,11 @@ CodeMirror.registerHelper(
5758
let ast;
5859
try {
5960
ast = jsonParse(text);
60-
} catch (syntaxError) {
61-
if (syntaxError.stack) {
62-
throw syntaxError;
61+
} catch (error) {
62+
if (error instanceof JSONSyntaxError) {
63+
return [lintError(editor, error.position, error.message)];
6364
}
64-
return [lintError(editor, syntaxError, syntaxError.message)];
65+
throw error;
6566
}
6667

6768
// If there are not yet known variables, do nothing.

packages/graphiql/src/components/ToolbarButton.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export class ToolbarButton extends React.Component<
4949
this.props.onClick();
5050
this.setState({ error: null });
5151
} catch (error) {
52-
this.setState({ error });
52+
if (error instanceof Error) {
53+
this.setState({ error });
54+
return;
55+
}
56+
throw error;
5357
}
5458
};
5559
}

packages/graphql-language-service-cli/src/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ switch (command) {
130130
startServer(options);
131131
} catch (error) {
132132
const logger = new Logger();
133-
logger.error(error);
133+
logger.error(String(error));
134134
}
135135
break;
136136
default: {

packages/graphql-language-service-cli/src/client.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ interface AutocompleteResultsMap {
7979
[i: number]: CompletionItem;
8080
}
8181

82+
function formatUnknownError(error: unknown) {
83+
let message: string | undefined;
84+
if (error instanceof Error) {
85+
message = error.stack;
86+
}
87+
return message ?? String(error);
88+
}
89+
8290
function _getAutocompleteSuggestions(
8391
queryText: string,
8492
point: Position,
@@ -104,7 +112,7 @@ function _getAutocompleteSuggestions(
104112
process.stdout.write(JSON.stringify(resultObject, null, 2));
105113
return GRAPHQL_SUCCESS_CODE;
106114
} catch (error) {
107-
process.stderr.write((error?.stack ?? String(error)) + '\n');
115+
process.stderr.write(formatUnknownError(error) + '\n');
108116
return GRAPHQL_FAILURE_CODE;
109117
}
110118
}
@@ -133,7 +141,7 @@ function _getDiagnostics(
133141
process.stdout.write(JSON.stringify(resultObject, null, 2));
134142
return GRAPHQL_SUCCESS_CODE;
135143
} catch (error) {
136-
process.stderr.write((error?.stack ?? String(error)) + '\n');
144+
process.stderr.write(formatUnknownError(error) + '\n');
137145
return GRAPHQL_FAILURE_CODE;
138146
}
139147
}
@@ -147,7 +155,7 @@ function _getOutline(queryText: string): EXIT_CODE {
147155
throw Error('Error parsing or no outline tree found');
148156
}
149157
} catch (error) {
150-
process.stderr.write((error?.stack ?? String(error)) + '\n');
158+
process.stderr.write(formatUnknownError(error) + '\n');
151159
return GRAPHQL_FAILURE_CODE;
152160
}
153161
return GRAPHQL_SUCCESS_CODE;
@@ -161,7 +169,7 @@ function ensureText(queryText: string, filePath: string): string {
161169
try {
162170
text = fs.readFileSync(filePath, 'utf8');
163171
} catch (error) {
164-
throw new Error(error);
172+
throw new Error(String(error));
165173
}
166174
}
167175
return text;

packages/graphql-language-service-server/src/GraphQLLanguageService.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TypeDefinitionNode,
1515
NamedTypeNode,
1616
ValidationRule,
17+
GraphQLError,
1718
} from 'graphql';
1819

1920
import {
@@ -153,15 +154,22 @@ export class GraphQLLanguageService {
153154
});
154155
}
155156
} catch (error) {
156-
const range = getRange(error.locations[0], document);
157-
return [
158-
{
159-
severity: DIAGNOSTIC_SEVERITY.Error,
160-
message: error.message,
161-
source: 'GraphQL: Syntax',
162-
range,
163-
},
164-
];
157+
if (error instanceof GraphQLError) {
158+
const range = getRange(
159+
error.locations?.[0] ?? { column: 0, line: 0 },
160+
document,
161+
);
162+
return [
163+
{
164+
severity: DIAGNOSTIC_SEVERITY.Error,
165+
message: error.message,
166+
source: 'GraphQL: Syntax',
167+
range,
168+
},
169+
];
170+
}
171+
172+
throw error;
165173
}
166174

167175
// If there's a matching config, proceed to prepare to run validation

packages/graphql-language-service-server/src/MessageProcessor.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export class MessageProcessor {
256256
}
257257
}
258258
} catch (err) {
259-
this._logger.warn(err);
259+
this._logger.warn(String(err));
260260
}
261261

262262
// Here, we set the workspace settings in memory,
@@ -834,7 +834,7 @@ export class MessageProcessor {
834834
await this._updateObjectTypeDefinition(uri, contents);
835835
}
836836
} catch (err) {
837-
this._logger.error(err);
837+
this._logger.error(String(err));
838838
}
839839
}
840840
async _cacheSchemaFile(
@@ -1002,7 +1002,7 @@ export class MessageProcessor {
10021002
}
10031003
}
10041004
} catch (err) {
1005-
this._logger.error(err);
1005+
this._logger.error(String(err));
10061006
}
10071007
}
10081008
/**
@@ -1043,7 +1043,7 @@ export class MessageProcessor {
10431043
this._logger.error(
10441044
`invalid/unknown file in graphql config documents entry:\n '${project.documents}'`,
10451045
);
1046-
this._logger.error(err);
1046+
this._logger.error(String(err));
10471047
}
10481048
}
10491049
/**

packages/graphql-language-service-server/src/findGraphQLTags.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function findGraphQLTags(
9595
logger.error(
9696
`Could not parse the ${type} file at ${uri} to extract the graphql tags:`,
9797
);
98-
logger.error(error);
98+
logger.error(String(error));
9999
return [];
100100
}
101101
const ast = parsedAST!;

packages/graphql-language-service-server/src/startServer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export default async function startServer(
211211
serverWithHandlers.listen();
212212
} catch (err) {
213213
logger.error('There was a Graphql LSP handler exception:');
214-
logger.error(err);
214+
logger.error(String(err));
215215
}
216216
}
217217
}
@@ -236,7 +236,7 @@ async function initializeHandlers({
236236
return connection;
237237
} catch (err) {
238238
logger.error('There was an error initializing the server connection');
239-
logger.error(err);
239+
logger.error(String(err));
240240
process.exit(1);
241241
}
242242
}

packages/graphql-language-service/src/interface/getDiagnostics.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,22 @@ export function getDiagnostics(
8181
try {
8282
ast = parse(query);
8383
} catch (error) {
84-
const range = getRange(error.locations[0], query);
85-
return [
86-
{
87-
severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity,
88-
message: error.message,
89-
source: 'GraphQL: Syntax',
90-
range,
91-
},
92-
];
84+
if (error instanceof GraphQLError) {
85+
const range = getRange(
86+
error.locations?.[0] ?? { line: 0, column: 0 },
87+
query,
88+
);
89+
90+
return [
91+
{
92+
severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity,
93+
message: error.message,
94+
source: 'GraphQL: Syntax',
95+
range,
96+
},
97+
];
98+
}
99+
throw error;
93100
}
94101

95102
return validateQuery(ast, schema, customRules, isRelayCompatMode);

packages/graphql-language-service/src/parser/Rules.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export const ParseRules: { [name: string]: ParseRule } = {
196196
StringValue: [
197197
{
198198
style: 'string',
199-
match: token => token.kind === 'String',
199+
match: (token: Token) => token.kind === 'String',
200200
update(state: State, token: Token) {
201201
if (token.value.startsWith('"""')) {
202202
state.inBlockstring = !token.value.slice(3).endsWith('"""');

0 commit comments

Comments
 (0)