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

Workaround an issue preventing users to catch errors from #26656

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion modules/standard/JSON.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,12 @@ module JSON {
proc fromJson(jsonString: string, type loadAs): loadAs throws {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a warning to the docs about this requirement? Or maybe a compilerWarning guarded by !isDefaultInitializable(loadAs)?

var fileReader = openStringReader(jsonString,
deserializer=new jsonDeserializer());
return fileReader.read(loadAs);
// we want `return fileReader.read(loadAs)`. But that ends up using a
// non-throwing compiler-generated initializer. That prevents the user to
// catch errors that are due to malformed jsonString.
var ret: loadAs;
fileReader.read(ret);
return ret;
}

/* Given a Chapel value, serialize it into a JSON string using the
Expand Down
20 changes: 20 additions & 0 deletions test/io/serializers/jsonHelpers/fromJsonThrows.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import IO, JSON;

record myRec {
var field: int;
}

var inFile = IO.open("fromJsonThrows.in", IO.ioMode.r);
var inReader = inFile.reader();

var rec: myRec;

proc main() throws {
try {
rec = JSON.fromJson(inReader.readAll(string), myRec);
writeln(rec);
}
catch err: IllegalArgumentError {
writeln("Caught the error correctly");
}
}
1 change: 1 addition & 0 deletions test/io/serializers/jsonHelpers/fromJsonThrows.cleanfiles
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fromJsonThrows.in
1 change: 1 addition & 0 deletions test/io/serializers/jsonHelpers/fromJsonThrows.good
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Caught the error correctly
3 changes: 3 additions & 0 deletions test/io/serializers/jsonHelpers/fromJsonThrows.preexec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "{\"fiel\":10}" > fromJsonThrows.in