Skip to content

Commit ca2119e

Browse files
DanTupCommit Queue
authored andcommitted
[anlaysis_server] Improve the error text/codes for property editor APIs
Change-Id: If3ad0fd11cc661e88104444afd14a5d79374cc1c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/406360 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Elliott Brooks <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 305388a commit ca2119e

File tree

3 files changed

+73
-36
lines changed

3 files changed

+73
-36
lines changed

pkg/analysis_server/lib/src/lsp/constants.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,21 @@ abstract final class ServerErrorCodes {
323323
/// General state error.
324324
static const StateError = ErrorCodes(-32015);
325325

326+
/// A request was made that requires use of workspace/applyEdit but the
327+
/// current editor does not support it.
328+
static const EditsUnsupportedByEditor = ErrorCodes(-32016);
329+
330+
/// An editArgument request tried to modify an invocation at a position where
331+
/// there was no invocation.
332+
static const EditArgumentInvalidPosition = ErrorCodes(-32017);
333+
334+
/// An editArgument request tried to modify a parameter that does not exist or
335+
/// is not editable.
336+
static const EditArgumentInvalidParameter = ErrorCodes(-32018);
337+
338+
/// An editArgument request tried to set an argument value that is not valid.
339+
static const EditArgumentInvalidValue = ErrorCodes(-32019);
340+
326341
/// An error raised when the server detects that the server and client are out
327342
/// of sync and cannot recover. For example if a textDocument/didChange notification
328343
/// has invalid offsets, suggesting the client and server have become out of sync

pkg/analysis_server/lib/src/lsp/handlers/custom/editable_arguments/handler_edit_argument.dart

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
5252

5353
if (!editorClientCapabilities.applyEdit) {
5454
return error(
55-
ErrorCodes.RequestFailed,
55+
ServerErrorCodes.EditsUnsupportedByEditor,
5656
'The connected editor does not support applying edits',
5757
);
5858
}
@@ -86,8 +86,8 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
8686
var invocation = getInvocationInfo(result, offset);
8787
if (invocation == null) {
8888
return error(
89-
ErrorCodes.RequestFailed,
90-
'No invocation was found at the position',
89+
ServerErrorCodes.EditArgumentInvalidPosition,
90+
'No invocation was found at the provided position',
9191
);
9292
}
9393

@@ -109,8 +109,8 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
109109
);
110110
if (parameter == null) {
111111
return error(
112-
ErrorCodes.RequestFailed,
113-
'Parameter "$parameterName" was not found in ${parameters.map((p) => p.name3).join(', ')}',
112+
ServerErrorCodes.EditArgumentInvalidParameter,
113+
"The parameter '$parameterName' was not found in this invocation. The available parameters are ${parameters.map((p) => p.name3).join(', ')}",
114114
);
115115
}
116116

@@ -129,9 +129,12 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
129129
// This should never happen unless a client is broken, because either
130130
// they should have failed the version check, or they would've already
131131
// known this argument was not editable.
132+
var notEditableReasonLower =
133+
notEditableReason.substring(0, 1).toLowerCase() +
134+
notEditableReason.substring(1);
132135
return error(
133-
ErrorCodes.RequestFailed,
134-
"Parameter '$parameterName' is not editable: $notEditableReason",
136+
ServerErrorCodes.EditArgumentInvalidParameter,
137+
"The parameter '$parameterName' is not editable because $notEditableReasonLower",
135138
);
136139
}
137140

@@ -183,8 +186,8 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
183186
return success('null');
184187
} else {
185188
return error(
186-
ErrorCodes.RequestFailed,
187-
'Value for non-nullable parameter "${edit.name}" cannot be null',
189+
ServerErrorCodes.EditArgumentInvalidValue,
190+
"The value for the parameter '${edit.name}' cannot be null",
188191
);
189192
}
190193
}
@@ -213,14 +216,14 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
213216
return success(value.toString());
214217
} else {
215218
return error(
216-
ErrorCodes.RequestFailed,
217-
'Value for parameter "${edit.name}" should be one of ${allowedValues.map((v) => '"$v"').join(', ')} but was "$value"',
219+
ServerErrorCodes.EditArgumentInvalidValue,
220+
"The value for the parameter '${edit.name}' should be one of ${allowedValues.map((v) => "'$v'").join(', ')} but was '$value'",
218221
);
219222
}
220223
} else {
221224
return error(
222-
ErrorCodes.RequestFailed,
223-
'Value for parameter "${edit.name}" should be $type but was ${value.runtimeType}',
225+
ServerErrorCodes.EditArgumentInvalidValue,
226+
"The value for the parameter '${edit.name}' should be $type but was ${value.runtimeType}",
224227
);
225228
}
226229
}
@@ -312,7 +315,7 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
312315
if (editResponse.error != null) {
313316
return error(
314317
ServerErrorCodes.ClientFailedToApplyEdit,
315-
'Client failed to apply workspace edit: $editDescription',
318+
"The editor failed to apply the workspace edit '$editDescription'",
316319
editResponse.error.toString(),
317320
);
318321
}
@@ -331,7 +334,7 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
331334
// changed.
332335
return error(
333336
ServerErrorCodes.ClientFailedToApplyEdit,
334-
'Client did not apply workspace edit: $editDescription '
337+
"The editor did not apply the workspace edit '$editDescription' "
335338
'(reason: ${failureReason ?? 'not given'})',
336339
workspaceEdit.toString(),
337340
);

pkg/analysis_server/test/shared/shared_edit_argument_tests.dart

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:analysis_server/lsp_protocol/protocol.dart';
8+
import 'package:analysis_server/src/lsp/constants.dart';
89
import 'package:analyzer/src/test_utilities/test_code_format.dart';
910
import 'package:test/test.dart';
1011

@@ -225,9 +226,10 @@ mixin SharedEditArgumentTests
225226
params: '([int? x, int y = 10, int? z])',
226227
originalArgs: '(1)',
227228
edit: ArgumentEdit(name: 'z', newValue: 2),
229+
errorCode: ServerErrorCodes.EditArgumentInvalidParameter,
228230
message:
229-
"Parameter 'z' is not editable: "
230-
"A value for the 3rd parameter can't be added until a value for all preceding positional parameters have been added.",
231+
"The parameter 'z' is not editable because "
232+
"a value for the 3rd parameter can't be added until a value for all preceding positional parameters have been added.",
231233
);
232234
}
233235

@@ -236,9 +238,10 @@ mixin SharedEditArgumentTests
236238
params: '([int? x = 10, int? y])',
237239
originalArgs: '()',
238240
edit: ArgumentEdit(name: 'y', newValue: 2),
241+
errorCode: ServerErrorCodes.EditArgumentInvalidParameter,
239242
message:
240-
"Parameter 'y' is not editable: "
241-
"A value for the 2nd parameter can't be added until a value for all preceding positional parameters have been added.",
243+
"The parameter 'y' is not editable because "
244+
"a value for the 2nd parameter can't be added until a value for all preceding positional parameters have been added.",
242245
);
243246
}
244247

@@ -268,9 +271,10 @@ mixin SharedEditArgumentTests
268271
params: '(int? x, int? y, int? z)',
269272
originalArgs: '(1)',
270273
edit: ArgumentEdit(name: 'z', newValue: 2),
274+
errorCode: ServerErrorCodes.EditArgumentInvalidParameter,
271275
message:
272-
"Parameter 'z' is not editable: "
273-
"A value for the 3rd parameter can't be added until a value for all preceding positional parameters have been added.",
276+
"The parameter 'z' is not editable because "
277+
"a value for the 3rd parameter can't be added until a value for all preceding positional parameters have been added.",
274278
);
275279
}
276280

@@ -280,9 +284,10 @@ mixin SharedEditArgumentTests
280284
params: '(int? x, int? y)',
281285
originalArgs: '()',
282286
edit: ArgumentEdit(name: 'y', newValue: 2),
287+
errorCode: ServerErrorCodes.EditArgumentInvalidParameter,
283288
message:
284-
"Parameter 'y' is not editable: "
285-
"A value for the 2nd parameter can't be added until a value for all preceding positional parameters have been added.",
289+
"The parameter 'y' is not editable because "
290+
"a value for the 2nd parameter can't be added until a value for all preceding positional parameters have been added.",
286291
);
287292
}
288293

@@ -292,9 +297,10 @@ mixin SharedEditArgumentTests
292297
params: '(int? x, int? y, { int? z })',
293298
originalArgs: '(z: 1)',
294299
edit: ArgumentEdit(name: 'y', newValue: 2),
300+
errorCode: ServerErrorCodes.EditArgumentInvalidParameter,
295301
message:
296-
"Parameter 'y' is not editable: "
297-
"A value for the 2nd parameter can't be added until a value for all preceding positional parameters have been added.",
302+
"The parameter 'y' is not editable because "
303+
"a value for the 2nd parameter can't be added until a value for all preceding positional parameters have been added.",
298304
);
299305
}
300306

@@ -358,7 +364,8 @@ mixin SharedEditArgumentTests
358364
params: '({ bool? x })',
359365
originalArgs: '(x: true)',
360366
edit: ArgumentEdit(name: 'x', newValue: 'invalid'),
361-
message: 'Value for parameter "x" should be bool? but was String',
367+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
368+
message: "The value for the parameter 'x' should be bool? but was String",
362369
);
363370
}
364371

@@ -376,7 +383,8 @@ mixin SharedEditArgumentTests
376383
params: '({ required bool x })',
377384
originalArgs: '(x: true)',
378385
edit: ArgumentEdit(name: 'x'),
379-
message: 'Value for non-nullable parameter "x" cannot be null',
386+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
387+
message: "The value for the parameter 'x' cannot be null",
380388
);
381389
}
382390

@@ -403,7 +411,9 @@ mixin SharedEditArgumentTests
403411
params: '({ double? x })',
404412
originalArgs: '(x: 1.1)',
405413
edit: ArgumentEdit(name: 'x', newValue: 'invalid'),
406-
message: 'Value for parameter "x" should be double? but was String',
414+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
415+
message:
416+
"The value for the parameter 'x' should be double? but was String",
407417
);
408418
}
409419

@@ -421,7 +431,8 @@ mixin SharedEditArgumentTests
421431
params: '({ required double x })',
422432
originalArgs: '(x: 1.0)',
423433
edit: ArgumentEdit(name: 'x'),
424-
message: 'Value for non-nullable parameter "x" cannot be null',
434+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
435+
message: "The value for the parameter 'x' cannot be null",
425436
);
426437
}
427438

@@ -467,8 +478,9 @@ mixin SharedEditArgumentTests
467478
params: '({ E? x })',
468479
originalArgs: '(x: E.one)',
469480
edit: ArgumentEdit(name: 'x', newValue: 'invalid'),
481+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
470482
message:
471-
'Value for parameter "x" should be one of "E.one", "E.two" but was "invalid"',
483+
"The value for the parameter 'x' should be one of 'E.one', 'E.two' but was 'invalid'",
472484
);
473485
}
474486

@@ -488,7 +500,8 @@ mixin SharedEditArgumentTests
488500
params: '({ required E x })',
489501
originalArgs: '(x: E.one)',
490502
edit: ArgumentEdit(name: 'x'),
491-
message: 'Value for non-nullable parameter "x" cannot be null',
503+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
504+
message: "The value for the parameter 'x' cannot be null",
492505
);
493506
}
494507

@@ -520,7 +533,8 @@ const myConst = E.one;
520533
params: '({ int? x })',
521534
originalArgs: '(x: 1)',
522535
edit: ArgumentEdit(name: 'x', newValue: 'invalid'),
523-
message: 'Value for parameter "x" should be int? but was String',
536+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
537+
message: "The value for the parameter 'x' should be int? but was String",
524538
);
525539
}
526540

@@ -538,7 +552,8 @@ const myConst = E.one;
538552
params: '({ required int x })',
539553
originalArgs: '(x: 1)',
540554
edit: ArgumentEdit(name: 'x'),
541-
message: 'Value for non-nullable parameter "x" cannot be null',
555+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
556+
message: "The value for the parameter 'x' cannot be null",
542557
);
543558
}
544559

@@ -592,7 +607,8 @@ const myConst = E.one;
592607
params: '({ String? x })',
593608
originalArgs: "(x: 'a')",
594609
edit: ArgumentEdit(name: 'x', newValue: 123),
595-
message: 'Value for parameter "x" should be String? but was int',
610+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
611+
message: "The value for the parameter 'x' should be String? but was int",
596612
);
597613
}
598614

@@ -619,7 +635,8 @@ const myConst = E.one;
619635
params: '({ required String x })',
620636
originalArgs: "(x: 'a')",
621637
edit: ArgumentEdit(name: 'x'),
622-
message: 'Value for non-nullable parameter "x" cannot be null',
638+
errorCode: ServerErrorCodes.EditArgumentInvalidValue,
639+
message: "The value for the parameter 'x' cannot be null",
623640
);
624641
}
625642

@@ -720,6 +737,7 @@ const myConst = E.one;
720737
params: '({ required String x })',
721738
originalArgs: "(x: 'a')",
722739
edit: ArgumentEdit(name: 'x'),
740+
errorCode: ServerErrorCodes.EditsUnsupportedByEditor,
723741
message: 'The connected editor does not support applying edits',
724742
);
725743
}
@@ -753,6 +771,7 @@ const myConst = E.one;
753771
required String params,
754772
required String originalArgs,
755773
required ArgumentEdit edit,
774+
required ErrorCodes errorCode,
756775
required String message,
757776
String? additionalCode,
758777
}) async {
@@ -777,7 +796,7 @@ class MyWidget extends StatelessWidget {
777796

778797
await expectLater(
779798
editArgument(testFileUri, code.position.position, edit),
780-
throwsA(isResponseError(ErrorCodes.RequestFailed, message: message)),
799+
throwsA(isResponseError(errorCode, message: message)),
781800
);
782801
}
783802

0 commit comments

Comments
 (0)