Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when JSON/YAML parsing would set property default in a Dynamic #700

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

HT154
Copy link
Contributor

@HT154 HT154 commented Oct 16, 2024

Resolves #561

@HT154 HT154 force-pushed the parse-dynamic-disallow-default branch from 8a2b659 to 109101e Compare October 16, 2024 17:44
Copy link
Contributor

@bioball bioball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Just some comments about error messages.

CC @holzensp @stackoverflow; merging this is a breaking change.

@HT154 HT154 requested a review from bioball October 16, 2024 23:23
Copy link
Contributor

@bioball bioball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; pending thumbs from @stackoverflow and @holzensp

@@ -469,6 +473,13 @@ private void addMembers(MappingNode node, VmObject object) {
var memberName =
convertedKey instanceof String string && !useMapping ? Identifier.get(string) : null;

// https://github.com/apple/pkl/issues/561
if (memberName != null && "default".equals(convertedKey)) {
throw new ParseException(
Copy link
Contributor

@odenix odenix Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other error handling code in this file throws YamlEngineException, which is caught in the same file and translated to VmException. Why throw a custom exception here, only to duplicate the code that translates to VmException? (I know this was suggested in the review, but I don't see the point.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YamlEngineException doesn't feel like the right class to throw our own errors with tailored messages. I think the direction we should move here is to switch all of our own throws from YamlEngineException to org.pkl.core.util.yaml.ParseException.

Copy link
Contributor

@odenix odenix Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing ParseException everywhere is an improvement. But since these exceptions are implementation details and thrown and caught in the same file, introducing your own exception feels unnecessary (adds code for no benefit).

json.ParserNodes follows the same pattern: It throws org.pkl.core.util.json.ParseException, which also isn't your own exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the extra class seems needlessly verbose and that we should probably move away from YamlEngineExceptions. I don't quite understand org.pkl.core.util.json.ParseException not being "your own exception." What do you mean by that, @translatenix?

Is there any reason not to move org.pkl.core.util.json.ParseException up one level (into util) and use it for either?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that errors coming from ParseException can be presented to users as-is; because they are already tailored to be presented as error messages.

YamlEngineException messages come from SnakeYAML, and should be decorated with a generic "malformed yaml" message.

I think a good north star here is to not surface messages from YamlEngineException at all, and instead present all parse errors as our own tailored error messages. But, for now, it's helpful to have two different exception classes so we know what comes from us, and what comes from our parser library.

Copy link
Contributor

@odenix odenix Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand org.pkl.core.util.json.ParseException not being "your own exception." What do you mean by that, @translatenix?

org.pkl.core.util.json.ParseException comes from the external JSON library that was copied into Pkl's codebase. As such it's the equivalent of YamlEngineException .

YamlEngineException messages come from SnakeYAML, and should be decorated with a generic "malformed yaml" message.

If that's what you want, having your own exception makes sense, assuming you can't throw a VmException to begin with. You may want to make the same change for json/ParserNodes.

@HT154 HT154 force-pushed the parse-dynamic-disallow-default branch 2 times, most recently from e5504b5 to ed92c3e Compare October 17, 2024 20:01
Copy link
Contributor

@stackoverflow stackoverflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@holzensp holzensp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits, but no strong objections. Approving to unblock; exception discussion is worth seeing through, but we can also follow up.

@@ -469,6 +473,13 @@ private void addMembers(MappingNode node, VmObject object) {
var memberName =
convertedKey instanceof String string && !useMapping ? Identifier.get(string) : null;

// https://github.com/apple/pkl/issues/561
if (memberName != null && "default".equals(convertedKey)) {
throw new ParseException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the extra class seems needlessly verbose and that we should probably move away from YamlEngineExceptions. I don't quite understand org.pkl.core.util.json.ParseException not being "your own exception." What do you mean by that, @translatenix?

Is there any reason not to move org.pkl.core.util.json.ParseException up one level (into util) and use it for either?

@HT154 HT154 force-pushed the parse-dynamic-disallow-default branch 3 times, most recently from f5f96df to 13e3306 Compare October 26, 2024 23:41
@HT154 HT154 force-pushed the parse-dynamic-disallow-default branch from 13e3306 to 9ee33e3 Compare October 29, 2024 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

json.Parser should throw an error when useMapping = false and key "default" is encountered
5 participants