Skip to content

Commit e5504b5

Browse files
committed
Improved error messages
1 parent 109101e commit e5504b5

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

pkl-core/src/main/java/org/pkl/core/stdlib/json/ParserNodes.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.pkl.core.stdlib.ExternalMethod1Node;
2828
import org.pkl.core.stdlib.PklConverter;
2929
import org.pkl.core.util.EconomicMaps;
30+
import org.pkl.core.util.ErrorMessages;
3031
import org.pkl.core.util.Nullable;
3132
import org.pkl.core.util.json.JsonHandler;
3233
import org.pkl.core.util.json.JsonParser;
@@ -172,12 +173,13 @@ public void endObject(@Nullable EconomicMap<Object, ObjectMember> members) {
172173

173174
@Override
174175
public void startObjectValue(@Nullable EconomicMap<Object, ObjectMember> members, String name) {
175-
if (!useMapping && "default".equals(name)) {
176+
var identifier = Identifier.get(name);
177+
if (!useMapping && identifier == Identifier.DEFAULT) {
176178
// https://github.com/apple/pkl/issues/561
177179
throw new ParseException(
178-
"Cannot parse object key `default` into a `Dynamic` value", getLocation());
180+
ErrorMessages.create("jsonParseErrorDynamicPropertyDefault"), getLocation());
179181
}
180-
currPath.push(Identifier.get(name));
182+
currPath.push(identifier);
181183
}
182184

183185
@Override

pkl-core/src/main/java/org/pkl/core/stdlib/yaml/ParserNodes.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.pkl.core.stdlib.ExternalMethod1Node;
3232
import org.pkl.core.stdlib.PklConverter;
3333
import org.pkl.core.util.EconomicMaps;
34+
import org.pkl.core.util.ErrorMessages;
35+
import org.pkl.core.util.yaml.ParseException;
3436
import org.pkl.core.util.yaml.snake.YamlUtils;
3537
import org.snakeyaml.engine.v2.api.ConstructNode;
3638
import org.snakeyaml.engine.v2.api.Load;
@@ -115,6 +117,8 @@ private VmList doParseAll(VmTyped self, String text, String uri) {
115117
.build();
116118
}
117119
throw exceptionBuilder().evalError("yamlParseError").withHint(e.getMessage()).build();
120+
} catch (ParseException e) {
121+
throw exceptionBuilder().evalError("yamlParseError").withHint(e.getMessage()).build();
118122
}
119123

120124
return builder.build();
@@ -470,9 +474,10 @@ private void addMembers(MappingNode node, VmObject object) {
470474
convertedKey instanceof String string && !useMapping ? Identifier.get(string) : null;
471475

472476
// https://github.com/apple/pkl/issues/561
473-
if (memberName != null && "default".equals(convertedKey)) {
474-
throw new YamlEngineException(
475-
"Cannot parse object key `default` into a Dynamic value. Node: " + node);
477+
if (memberName == Identifier.DEFAULT) {
478+
throw new ParseException(
479+
ErrorMessages.create("yamlParseErrorDynamicPropertyDefault"),
480+
keyNode.getStartMark().isEmpty() ? null : keyNode.getStartMark().get());
476481
}
477482

478483
var member =
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.pkl.core.util.yaml;
17+
18+
import org.pkl.core.util.Nullable;
19+
import org.snakeyaml.engine.v2.exceptions.Mark;
20+
21+
/** An unchecked exception to indicate that an input does not qualify as valid YAML. */
22+
public final class ParseException extends RuntimeException {
23+
private final @Nullable Mark location;
24+
25+
public ParseException(String message, @Nullable Mark location) {
26+
super(location == null ? message : message + location);
27+
this.location = location;
28+
}
29+
30+
/**
31+
* Returns the location at which the error occurred.
32+
*
33+
* @return the error location
34+
*/
35+
public @Nullable Mark getLocation() {
36+
return location;
37+
}
38+
}

pkl-core/src/main/resources/org/pkl/core/errorMessages.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,11 @@ Converter path `{0}` has invalid syntax.
790790
jsonParseError=\
791791
Error parsing JSON document.
792792

793+
jsonParseErrorDynamicPropertyDefault=\
794+
Cannot parse object with key `default` into ` Dynamic`.\
795+
\n\
796+
Try parsing into `Mapping` instead with `useMapping = true` in `pkl.json#Parser`.
797+
793798
yamlParseError=\
794799
Error parsing YAML document.
795800

@@ -798,6 +803,11 @@ Error parsing YAML document: The number of aliases for collection nodes exceeds
798803
\n\
799804
To increase the allowed maximum, set `YamlRenderer.maxCollectionAliases`.
800805

806+
yamlParseErrorDynamicPropertyDefault=\
807+
Cannot parse object with key `default` into ` Dynamic`.\
808+
\n\
809+
Try parsing into `Mapping` instead with `useMapping = true` in `pkl.yaml#Parser`.
810+
801811
evaluationTimedOut=\
802812
Evaluation timed out after {0,number,#.##} second(s).
803813

0 commit comments

Comments
 (0)