tag:blogger.com,1999:blog-1271763227002553835.post3004927558427473893..comments2024-03-26T08:42:34.744+01:00Comments on Notes on Oracle: APEX: Friendlier exception message from AJAX callAlex Nuijtenhttp://www.blogger.com/profile/06345615264010120428noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-1271763227002553835.post-55681286514270718832019-09-03T09:11:53.582+02:002019-09-03T09:11:53.582+02:00when used it, I faced error (Error: parsererror - ...when used it, I faced error (Error: parsererror - SyntaxError: Unexpected token { in JSON at position 37);<br />my code is, <br />DECLARE <br />V_SI_TYPE NUMBER; <br /><br />BEGIN<br /><br />SELECT 1 INTO V_SI_TYPE FROM DUAL WHERE EXISTS(SELECT ID FROM SALE_INVOICE_HDR WHERE ID = TO_NUMBER(:P24_SI_ID));<br /><br />IF V_SI_TYPE IS NULL <br />THEN<br />SELECT 2 INTO V_SI_TYPE FROM DUAL WHERE EXISTS(SELECT ID FROM DELIVERY_SALE_INVOICE_HDR WHERE ID = TO_NUMBER(:P24_SI_ID));<br />END IF; <br /><br />IF V_SI_TYPE IS NULL <br />THEN<br />SELECT 3 INTO V_SI_TYPE FROM DUAL WHERE EXISTS(SELECT ID FROM TAKEAWAY_INVOICE_HDR WHERE ID = TO_NUMBER(:P24_SI_ID));<br />END IF; <br /><br />RETURN V_SI_TYPE;<br />EXCEPTION<br />WHEN NO_DATA_FOUND<br />THEN <br /> htp.p ('{"error":"Department has Employees"}');<br />RETURN '';<br /><br />END; semsemhttps://www.blogger.com/profile/09941551466094033762noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-31035883016464588522016-03-06T22:31:52.824+01:002016-03-06T22:31:52.824+01:00You have the same effect on your demo_app. Try to ...You have the same effect on your demo_app. Try to delete a department and have a look into firebug.Christophhttps://www.blogger.com/profile/03512688323663687539noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-62436399442444445062016-03-06T22:26:02.576+01:002016-03-06T22:26:02.576+01:00When i use htp.p('{"error":"My ...When i use htp.p('{"error":"My Error"}') in my PL/SQL Block called by a dynamic action it doesnt work anymore.<br /><br />Reason for this is an extra status line in the response. My complete response looks like<br /><br />{"error":"My Error"}<br />Status: 204 No Content<br /><br />Seems like a bug, isn't ?<br /><br />I'm using APEX 5.0.1.00.06 on Oracle 12cChristophhttps://www.blogger.com/profile/03512688323663687539noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-70957433277148598972014-11-04T15:16:15.582+01:002014-11-04T15:16:15.582+01:00If you want to use a custom error handler instead ...If you want to use a custom error handler instead of the ugly alert <br />popup error message, you can do so by defining your own javascript <br />function called "onerror". See example below<br /><br />Tested with APEX 4.2.5 and APEX 5 EA2<br /><br />Filip van Vooren<br /><br /><br /><br /> /******************************************************************<br /> * Function : Custom APEX Dynamic Action (AJAX) error handler<br /> * Parameter : p_message = Error message to display<br /> *<br /> * Remarks<br /> * If a Dynamic Action of type (PL/SQL) causes an exception, than<br /> * the APEX ajax call error handler will check if the "window.onerror" <br /> * function exists for the current window.<br /> * <br /> * If that is the case it will be called accordingly, otherwise<br /> * an alert() popup will be generated with the corresponding error.<br /> *<br /> * On PL/SQL side you can influence the error message by writing JSON<br /> * to the HTTP buffer: htp.p('{ "error" : "my error text" }';<br /> ******************************************************************/<br /> function onerror(p_message) {<br /> // My fancy error handling goes here<br /> } Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-17176339522985935372013-10-30T15:26:20.255+01:002013-10-30T15:26:20.255+01:00Hi JR, Thank you for sharing.Hi JR, Thank you for sharing.Alex Nuijtenhttps://www.blogger.com/profile/06345615264010120428noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-10839558285837792912013-10-30T11:42:46.942+01:002013-10-30T11:42:46.942+01:00Hi Alex,
I've done some more investigation on...Hi Alex,<br /><br />I've done some more investigation on the issue I was experiencing, and I thought I'd post a quick update with my findings. In summary if you want to output a custom error message during an Ajax call, as described in your original post, you can't set the "Page Items to Return" attribute in the Dynamic Action. If you do, an "exception: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data" is provoked in out of the box Apex *.js file, as I've identified previously.<br /><br />Cheers,<br /><br />JR.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-46532404330721822052013-10-15T17:58:36.838+02:002013-10-15T17:58:36.838+02:00Hi Alex,
I don't have a RAISE in the exceptio...Hi Alex,<br /><br />I don't have a RAISE in the exception handler unfortunately, so that isn't my problem.<br /><br />I fear I may have confused matters when I mentiuoned the call I was making to RAISE_APPLICATION_ERROR. The bottom line is that if the only statement I have in the Exception handling block is: <br /><br />htp.p ('{"error":"Department has Employees"}'); <br /><br />The exception: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data ...sFunction(h.promise)?h.promise().then(d.resolve,d.reject):d[g](h)}):b[a](d[g])})... apex_4_1.min.js (line 16) is thrown in the browser and the expected Alert is not displayed.<br /><br />I very much appreciate your responses. I can only assume the issue is related to the code in apex_4_1.min.js, which must be different to the version you have tested with both currently an previously.<br /><br />JR.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-39022908599803602732013-10-15T14:37:10.627+02:002013-10-15T14:37:10.627+02:00Hi JR,
Do you happen to have a RAISE after the htp...Hi JR,<br />Do you happen to have a RAISE after the htp.p call?<br />If I place a RAISE; after the htp.p ('{"error":"Department has Employees"}'); I get a similar message a you describe.<br />The response that is returned (with the RAISE included) is as follows - you can see this in firebug:<br />{"error":"Department has Employees"}<br />{"error":"AJAX call returned server error ORA-02292: integrity constraint (NUIJTEN.SYS_C006303602) violated - child record found for Execute PL\u002FSQL Code."}<br /><br />Removing the RAISE in the exception handler did the trick (in my case)Alex Nuijtenhttps://www.blogger.com/profile/06345615264010120428noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-23419685497914669622013-10-15T10:09:38.917+02:002013-10-15T10:09:38.917+02:00Hi, thanks for your reply.
I've now tested i...Hi, thanks for your reply. <br /><br />I've now tested in Firefox and experience the same problem. Without specifying a call to htp.p in the exception hanlding block I can see from Firebug that the default JSON return string is of the format {"error":"AJAX call returned server error Execute PL\/SQL Code."}. Which presents the deafult alert to the user, as expected.<br /><br />However, when I call the htp.p function in the exception handling block of my code e.g. htp.p('{"error":"test"}'); I see the following exception thrown in Firebug: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data ...sFunction(h.promise)?h.promise().then(d.resolve,d.reject):d[g](h)}):b[a](d[g])})... apex_4_1.min.js (line 16)<br /><br />Therefore I presume the return from htp.p is not being handled correctly by the enclosing call block. Unfortunately I'm not sure how you can get around this issue as the <br />apex_4_1.min.js is minimised, and also I'm not confortable that modifying the out of the box Apex JS is a workable solution.<br /><br />I have found that raising an application error in the exception handling block gives some control over the error message output, e.g. RAISE_APPLICATION_ERROR(-20001, 'My custom error text'); results in output in the alert dialog of the format "AJAX call returned server error ORA-20001: My custom error text for Execute PL/SQL Code.", but this is of course not as desirable as your original solution.<br /><br />I'd be grateful if you have any other ideas on what the issue might be, or how to work around it.<br /><br />Many thanks,<br /><br />JRAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-56947495456228090672013-10-11T15:46:49.408+02:002013-10-11T15:46:49.408+02:00Yes, I believe it should wok in APEX 4.1 - however...Yes, I believe it should wok in APEX 4.1 - however I don't have the ability to test it, so can't verify. You could try with the developers tools from IE8 to debug it. Does it work in other browsers, like Chrome or Firefox, or does it show an error as well?Alex Nuijtenhttps://www.blogger.com/profile/06345615264010120428noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-19457346587552500572013-10-11T14:53:13.844+02:002013-10-11T14:53:13.844+02:00Hi, is this implementation expected to function in...Hi, is this implementation expected to function in Apex 4.1? I'm currently receiving the following error message in IE 8:<br /><br />apex_4_1.min.js<br />Message: Syntax error<br />Line: 16<br />Char: 11695<br />Code: 0<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-71997969200373041182013-04-25T10:57:05.632+02:002013-04-25T10:57:05.632+02:00Are you using a stored procedure to retrieve the v...Are you using a stored procedure to retrieve the values from the database and place them in Page Items? Then you can use exception handling as described.Alex Nuijtenhttps://www.blogger.com/profile/06345615264010120428noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-53906598373578975692013-04-25T10:33:28.237+02:002013-04-25T10:33:28.237+02:00Hi Alex, I am also facing the same problem. i have...Hi Alex, I am also facing the same problem. i have Page Items to Return in Dynamic Action. What i need to do in this situationRohit Chaurasiahttps://www.blogger.com/profile/02628155548625804294noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-56513966641356053452013-04-09T18:06:11.336+02:002013-04-09T18:06:11.336+02:00Nice! Just thought I'd add that it doesn'...Nice! Just thought I'd add that it doesn't appear to work if you have Page Items to Return.Big Bhttps://www.blogger.com/profile/13462764859723721308noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-42414138200194494412013-04-08T09:10:13.510+02:002013-04-08T09:10:13.510+02:00I'm not even sure that there is documentation ...I'm not even sure that there is documentation how browsers interpret these JSON strings. By examining the response which caused the original error message, I modified the error which is being returned from the stored procedure.<br />Alex Nuijtenhttps://www.blogger.com/profile/06345615264010120428noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-37871876810826297572013-04-05T21:51:14.318+02:002013-04-05T21:51:14.318+02:00Alex, can you point me to some documentation on ho...Alex, can you point me to some documentation on how browsers interpret these JSON strings? I can't find anything, but maybe I'm not using the right search terms. Are there other possibilities beyond just {"error":""}?Toby Markshttps://www.blogger.com/profile/09367177211408746652noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-6704380415615892092013-01-31T10:18:02.079+01:002013-01-31T10:18:02.079+01:00Instead of returning the AJAX- exception (the serv...Instead of returning the AJAX- exception (the server error), the original exception is handled and propagated as a JSON message. The browser knows how to handle this and will show an alert with the message.Alex Nuijtenhttps://www.blogger.com/profile/06345615264010120428noreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-12816736253749655122013-01-30T17:17:25.065+01:002013-01-30T17:17:25.065+01:00Hi, could you tell me why do you use htp.p ('...Hi, could you tell me why do you use htp.p ('{"error": ?<br />What does it do?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1271763227002553835.post-26883528577979146422012-05-28T21:10:27.105+02:002012-05-28T21:10:27.105+02:00Great example.. Thank you very much.Great example.. Thank you very much.yavuzhttps://www.blogger.com/profile/16017260922340082776noreply@blogger.com