<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: handle exceptions SQLCode = -981 in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836734#M4867577</link>
    <description>&lt;P&gt;In my understanding, 403 and 404 and other 4xx and 5xx HTTP status codes are returned by the according webserver (and therefore can be returned with EXCEPTIONS=off, that's my experience as well), whereas the current topic is that the according web server itself is not reachable at all.&lt;/P&gt;</description>
    <pubDate>Wed, 06 Dec 2023 07:11:24 GMT</pubDate>
    <dc:creator>VolkerBarth</dc:creator>
    <dc:date>2023-12-06T07:11:24Z</dc:date>
    <item>
      <title>handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaq-p/13836725</link>
      <description>&lt;P&gt;How is it possible to catch errors arising due to unreachable remote host?&lt;/P&gt;
&lt;PRE&gt;create or replace function myfunction (mysocket varchar(50))
returns long varchar
url 'http://!mysocket/myendpoint'
type 'HTTP:GET'
set 'HTTP(VERSION=1.1)';
&lt;/PRE&gt;

&lt;P&gt;Why I cant catch the error here (when the host is unreachable):
&lt;/P&gt;&lt;PRE&gt;begin
select myfunction ('unreachablehost:unreachableport')
exception when others then
SELECT SQLCODE, SQLSTATE
end;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I get the error message:
SQLCODE=-981, ODBC 3-Status=HY000"&lt;/P&gt;
&lt;P&gt;Is there anyway to catch the error in myfunction?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2023 03:33:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaq-p/13836725</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2023-12-05T03:33:36Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836726#M4867569</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Is there anyway to catch the error in myfunction?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think so: In order to handle exceptions, you need to have a code block with an error handler via TRY/CATCH or an EXCEPTIONS clause. In contrast to regular functions and procedures, web client functions and procedures do not have a code block as body but just the URL so there's no "place for that".&lt;/P&gt;
&lt;P&gt;The common solution would be to use a wrapper function/procedure that calls the web client function in a code block with exception handling - just like your sample. (And you will probably need such a wrapper function to supply parameters to the web client function and to extract information from its return values anyway...)&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;For certain kind of errors (like SOAP faults), you can use &lt;A href="https://dcx.sap.com/index.html#sqla170/en/html/816bd5766ce21014aee1f6b507ad3a39.html"&gt;the http option EXCEPTIONS&lt;/A&gt; set to OFF (such as "set HTTP(VERSION=1.1; EXCEPTIONS=OFF)' to get responses even when the request failed. But as to the docs, that won't work when the remote host is not available at all, as in your case.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2023 05:12:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836726#M4867569</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2023-12-05T05:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836729#M4867572</link>
      <description>&lt;P&gt;Thanks for your reply!&lt;/P&gt;
&lt;P&gt;In my case I go in a loop and call the wrapper function (in my case myfunction) against several remotes.&lt;/P&gt;
&lt;P&gt;What I want to achieve, is that even if one of the remotes is not reachable, then I need to continue my loop and jump to the next remote.&lt;/P&gt;
&lt;P&gt;Should I understand that this is not possible (in case the host is completely unreachable)?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2023 05:30:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836729#M4867572</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2023-12-05T05:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836730#M4867573</link>
      <description>&lt;P&gt;what about your suggested 'common solution', is it not the same as in my second block?&lt;/P&gt;
&lt;P&gt;Why I can't catch the error in my begin/end block?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2023 05:37:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836730#M4867573</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2023-12-05T05:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836727#M4867570</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;call the wrapper function (in my case myfunction)&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hm, in my terminology, myfunction is the &lt;EM&gt;web client function&lt;/EM&gt; (i.e. the one with the URL clause), not the &lt;EM&gt;wrapper function&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;A wrapper function "around the web client function" could be something like that: Try to call the web client function once&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;and return the response in case of success&lt;/LI&gt;
&lt;LI&gt;or in case of an unavailable host, repeat the call (after a wait) some more times until the request succeeds&lt;/LI&gt;
&lt;LI&gt;or for different errors, return the error to the caller:&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;create or replace function myWrapperFunction()
   returns long varchar
begin
   declare exp_unable_to_connect_to_host exception for sqlstate 'WW050';
   declare bTryAgain bit = 1;
   declare nMaxRetries int = 5;
   declare tmWaitSpan time = '00:00:05'; -- 5 s
   declare nCntAttempts int = 0;
   declare strResult long varchar;

   while bTryAgain = 1 and nCntAttempts &amp;lt; nMaxRetries loop
   begin
      set nCntAttempts = nCntAttempts + 1;
      set strResult = myfunction ('unreachablehost:1234');
      set bTryAgain = 0;
      exception
         -- remote host unavailable: wait and try again
         when exp_unable_to_connect_to_host then
            message 'myWrapperFunction: SQLSTATE set to ', sqlstate to console;
            waitfor delay tmWaitSpan;
         -- other errors: log and 
         when others then
            message 'myWrapperFunction: SQLSTATE set to ', sqlstate to console;
            set bTryAgain = 0;
            resignal; -- escalate error to caller of the function
   end;
   end loop;
   return strResult;
end;
select myWrapperFunction();

&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Dec 2023 06:17:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836727#M4867570</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2023-12-05T06:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836731#M4867574</link>
      <description>&lt;P&gt;&lt;A href="https://sqlanywhere-forum.sap.com/users/26/volker-barth"&gt;@volker Barth&lt;/A&gt;, thank you very much for the solution.&lt;/P&gt;
&lt;P&gt;Now I found the mistake why my second block was not working (I need another set of begin/end) so it works too:&lt;/P&gt;
&lt;PRE&gt;begin
declare strResult long varchar;
begin
set strResult  = (myfunction ('unreachablehost:unreachableport'));
exception when others then
set strResult  = '';
end;
select strResult 
end;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Dec 2023 07:24:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836731#M4867574</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2023-12-05T07:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836728#M4867571</link>
      <description>&lt;P&gt;I think what you really want to do is set myfunction so it returns HTTP error codes, then you determine the HTTP response, this is done by turning EXCEPTIONS off:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;set 'HTTP(EXCEPTIONS=off; VERSION=1.1)'
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;Then you can code up how to handle all the different 400 and 500 errors as well.&lt;/P&gt;
&lt;P&gt;From the &lt;EM&gt;CREATE FUNCTION statement [Web service]&lt;/EM&gt; documentation:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;EXCEPTIONS={ ON | OFF | AUTO } (short form EX) This HTTP option allows you to control status code handling. The default is ON.
When set to ON or AUTO, HTTP client functions will return a response for HTTP success status codes (1XX and 2XX) and all codes will raise the exception SQLE_HTTP_REQUEST_FAILED.
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 06 Dec 2023 04:20:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836728#M4867571</guid>
      <dc:creator>PCollins</dc:creator>
      <dc:date>2023-12-06T04:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836732#M4867575</link>
      <description>&lt;P&gt;A little caveat: The docs (see my answer) also note&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Exceptions that are not related to the HTTP status code (for example, SQLE_UNABLE_TO_CONNECT_TO_HOST) will be raised when appropriate regardless of the EXCEPTIONS setting.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So that particular error (SQLE_UNABLE_TO_CONNECT_TO_HOST, SQLCODE -981) still will throw an exception, and it is quite understandable in my book: If the host isn't available at all, there is no response, so there's no HTTP status to be returned "as is"...&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 04:50:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836732#M4867575</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2023-12-06T04:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836733#M4867576</link>
      <description>&lt;P&gt;That is not my experience, I get client HTTP errors reported including 404 and 403 reported&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 04:55:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836733#M4867576</guid>
      <dc:creator>PCollins</dc:creator>
      <dc:date>2023-12-06T04:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836734#M4867577</link>
      <description>&lt;P&gt;In my understanding, 403 and 404 and other 4xx and 5xx HTTP status codes are returned by the according webserver (and therefore can be returned with EXCEPTIONS=off, that's my experience as well), whereas the current topic is that the according web server itself is not reachable at all.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 07:11:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836734#M4867577</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2023-12-06T07:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: handle exceptions SQLCode = -981</title>
      <link>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836735#M4867578</link>
      <description>&lt;P&gt;Ah right, I see and sorry I misread, yes in that scenario we just handle with an exception as you have suggested in the wrapper - nice talking, ignore me I'll go back to sleep &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2023 04:33:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/handle-exceptions-sqlcode-981/qaa-p/13836735#M4867578</guid>
      <dc:creator>PCollins</dc:creator>
      <dc:date>2023-12-08T04:33:39Z</dc:date>
    </item>
  </channel>
</rss>

