Skip to content

Commit

Permalink
Improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
HT154 committed Oct 17, 2024
1 parent 109101e commit e5504b5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.pkl.core.stdlib.ExternalMethod1Node;
import org.pkl.core.stdlib.PklConverter;
import org.pkl.core.util.EconomicMaps;
import org.pkl.core.util.ErrorMessages;
import org.pkl.core.util.Nullable;
import org.pkl.core.util.json.JsonHandler;
import org.pkl.core.util.json.JsonParser;
Expand Down Expand Up @@ -172,12 +173,13 @@ public void endObject(@Nullable EconomicMap<Object, ObjectMember> members) {

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

@Override
Expand Down
11 changes: 8 additions & 3 deletions pkl-core/src/main/java/org/pkl/core/stdlib/yaml/ParserNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.pkl.core.stdlib.ExternalMethod1Node;
import org.pkl.core.stdlib.PklConverter;
import org.pkl.core.util.EconomicMaps;
import org.pkl.core.util.ErrorMessages;
import org.pkl.core.util.yaml.ParseException;
import org.pkl.core.util.yaml.snake.YamlUtils;
import org.snakeyaml.engine.v2.api.ConstructNode;
import org.snakeyaml.engine.v2.api.Load;
Expand Down Expand Up @@ -115,6 +117,8 @@ private VmList doParseAll(VmTyped self, String text, String uri) {
.build();
}
throw exceptionBuilder().evalError("yamlParseError").withHint(e.getMessage()).build();
} catch (ParseException e) {
throw exceptionBuilder().evalError("yamlParseError").withHint(e.getMessage()).build();
}

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

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

var member =
Expand Down
38 changes: 38 additions & 0 deletions pkl-core/src/main/java/org/pkl/core/util/yaml/ParseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.core.util.yaml;

import org.pkl.core.util.Nullable;
import org.snakeyaml.engine.v2.exceptions.Mark;

/** An unchecked exception to indicate that an input does not qualify as valid YAML. */
public final class ParseException extends RuntimeException {
private final @Nullable Mark location;

public ParseException(String message, @Nullable Mark location) {
super(location == null ? message : message + location);
this.location = location;
}

/**
* Returns the location at which the error occurred.
*
* @return the error location
*/
public @Nullable Mark getLocation() {
return location;
}
}
10 changes: 10 additions & 0 deletions pkl-core/src/main/resources/org/pkl/core/errorMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,11 @@ Converter path `{0}` has invalid syntax.
jsonParseError=\
Error parsing JSON document.

jsonParseErrorDynamicPropertyDefault=\
Cannot parse object with key `default` into ` Dynamic`.\
\n\
Try parsing into `Mapping` instead with `useMapping = true` in `pkl.json#Parser`.

yamlParseError=\
Error parsing YAML document.

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

yamlParseErrorDynamicPropertyDefault=\
Cannot parse object with key `default` into ` Dynamic`.\
\n\
Try parsing into `Mapping` instead with `useMapping = true` in `pkl.yaml#Parser`.

evaluationTimedOut=\
Evaluation timed out after {0,number,#.##} second(s).

Expand Down

0 comments on commit e5504b5

Please sign in to comment.