Bug #10381
READ-JSON dataset handle method doesn't throw error when parsing a invalid JSON
100%
Related issues
History
#1 Updated by Paul Bodale 12 months ago
Given the following file:
{"ds": {
"Main": [
"firstField": 413,
"field2": "1"
}
]
]
}
}}
The following code should throw a parsing JSON error but the function completes execution and the wrong result is returned.
BLOCK-LEVEL ON ERROR UNDO, THROW.
DEFINE TEMP-TABLE ttMain NO-UNDO SERIALIZE-NAME "Main"
FIELD field1 AS INTEGER SERIALIZE-NAME "firstField"
FIELD field2 AS CHARACTER.
DEFINE TEMP-TABLE ttSecondary NO-UNDO SERIALIZE-NAME "Secondary"
FIELD field1 AS CHARACTER SERIALIZE-NAME "field1"
FIELD field2 AS CHARACTER SERIALIZE-NAME "field2"
FIELD anotherField3 AS CHARACTER SERIALIZE-NAME "field3"
FIELD parentId AS RECID SERIALIZE-HIDDEN.
DEFINE DATASET ds FOR ttMain, ttSecondary
PARENT-ID-RELATION FOR ttMain, ttSecondary PARENT-ID-FIELD parentId.
FUNCTION readJson RETURNS LOGICAL ():
DEFINE VARIABLE fileContents AS LONGCHAR NO-UNDO.
COPY-LOB FROM FILE "task\\input\\incorectJson.txt" TO fileContents.
DATASET ds:READ-JSON("LONGCHAR", fileContents, "EMPTY").
RETURN TRUE.
CATCH e AS Progress.Lang.Error :
MESSAGE e:GetMessage(e:NumMessages).
RETURN FALSE.
END CATCH.
END FUNCTION.
DEFINE VARIABLE r AS LOGICAL NO-UNDO.
r = readJson().
MESSAGE "Success: " r.
#2 Updated by Paul Bodale 12 months ago
- Related to Bug #10352: Fix READ-JSON dataset handle method's behaviour added
#4 Updated by Teodor Gorghe 12 months ago
- Related to Bug #10284: JSON import error handing improvement added
#5 Updated by Paul Bodale 11 months ago
- % Done changed from 0 to 50
- Status changed from New to WIP
Committed rev. 16123 on branch 10381a.
I analyzed the situation and it seems like the problem might be related to the exception processing behavior of FWD. The problem is related to the dataset method and not the handle method like I initially thought. If instead of calling the READ-JSON method directly we do it by using the handle of the dataset like follows:
DEF VAR h AS HANDLE NO-UNDO.
h = DATASET ds:HANDLE.
h:READ-JSON("LONGCHAR", fileContents, "EMPTY").
Then an error is thrown and the execution stops correctly. I noticed that in first case, the READ-JSON method is executed via a proxy, the fact that the catch block was not executing and that the type of the error which was thrown was different than the one in the second case (when calling READ-JSON using the handle). After I discussed with Teodor, he proposed the following solution:
=== modified file 'src/com/goldencode/p2j/persist/StaticDataSet.java'
--- old/src/com/goldencode/p2j/persist/StaticDataSet.java 2024-09-11 10:32:12 +0000
+++ new/src/com/goldencode/p2j/persist/StaticDataSet.java 2025-08-19 11:42:48 +0000
@@ -512,7 +512,14 @@
StaticDataSet dsp = (StaticDataSet) proxy;
if (dsp.bound != null)
{
- return Utils.invoke(method, dsp.bound, args);
+ try
+ {
+ return Utils.invoke(method, dsp.bound, args);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw e.getCause() != null ? e.getCause() : e;
+ }
}
String[] errParam = new String[1];
This solves the better part of the problem this task was initially created for. The only thing left to do is to work on the error messages as they are not the correct ones.
#6 Updated by Paul Bodale 11 months ago
- % Done changed from 50 to 100
- Assignee set to Paul Bodale
- Status changed from WIP to Review
- reviewer Constantin Asofiei added
Committed rev. 16124 on branch 10381a that makes FWD throw the correct exceptions for invalid JSON parsing.
Please note the fact that not all errors that progress throws might be handled. In the event that the execution might encounter such a case, a new PersistenceException will be thrown and the handleJsonParseException method can easily be extended.
#7 Updated by Constantin Asofiei 10 months ago
Teodor - please add javadocs and history entries.
#8 Updated by Constantin Asofiei 10 months ago
- % Done changed from 100 to 90
- Status changed from Review to WIP
#10 Updated by Constantin Asofiei 10 months ago
Paul, please also add javadoc to handleJsonParseException
#12 Updated by Constantin Asofiei 10 months ago
- Status changed from Review to Merge Pending
Paul, please merge after 10371a.