HTTPRequest() please give back error message

Got an idea for enhancing SpiderBasic? New command(s) you'd like to see?
hoerbie
Posts: 100
Joined: Sun Mar 17, 2019 5:51 pm
Location: DE/BY/MUC

HTTPRequest() please give back error message

Post by hoerbie »

Hi,
actually (2.40) the HTTPRequest doesn't give back any information why the request failed.
In spider_HTTPRequest() there are three parameters in the $.ajax error:function(a,b,c) defined, but no-one is given back.
Looking in the docs, "b" gives back something like "timeout", "error", "abort" or "parsererror", and "c" gives back text http status like "Not Found" or "Internal Server Error."

For the callback to the SB program it is clear that the request was an error, when the first parameter is 0/#False, so the above error messages shouldn't be a problem when send as the second callback parameter. So maybe something like give back "c" when set, else give back "b"?
In the help it is written "The result of the request, as a text object", and an error message for me would be also a result.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: HTTPRequest() please give back error message

Post by Peter »

You can use the following workaround until then. Overwrite the SpiderBasic function and add the missing parameters to the callback:

Code: Select all

! spider_HTTPRequest = function (a, k, l, g, d, c) {
!   "undefined" === typeof d && (d = 0);
!   "undefined" === typeof c && (c = {
!     map: {}
!   });
!   for (var f = Object.keys(c.map), h = {}, b = "GET", e = 0; e < f.length; e++)
!     h[f[e]] = c.map[f[e]]._s;
!   $.support.cors = !0;
!   switch (a) {
!     case 1:
!       b = "POST";
!       break;
!     case 2:
!       b = "PUT";
!       break;
!     case 4:
!       b = "DELETE";
!       break;
!     case 3:
!       b = "PATCH"
!   }
!   $.ajax({
!     method: b,
!     cache: !1,
!     url: k,
!     data: l,
!     dataType: "html",
!     headers: h,
!     success: function (a) {
!       g(1, a, d)
!     },
!     error: function (a, b, c) {
!       g(0, "", d, a, b, c); // <- Here
!     }
!   })
! }

Procedure.s JSONstringify(JSONobject)
  ! return JSON.stringify(v_jsonobject);
EndProcedure

Procedure HttpGetEvent(Success, Result$, UserData, a, b, c)
  If Success
    Debug Result$
  Else
    Debug "HTTPRequest(): Error"
    Debug "a: " + JSONstringify(a)
    Debug "b: " + b
    Debug "c: " + c
  EndIf
EndProcedure

HTTPRequest(#PB_HTTP_Get, "NonExistingFileCausesFileNotFoundError", "", @HttpGetEvent())
hoerbie
Posts: 100
Joined: Sun Mar 17, 2019 5:51 pm
Location: DE/BY/MUC

Re: HTTPRequest() please give back error message

Post by hoerbie »

Peter, thanks for your direct solution, but much more for simply making this SB function readable for me. I think I will never get familiar with understanding such compressed spaghetti code, although programming for about 35 years in a lot of languages... But now it is readable and understandable, maybe I will try other things now!
Post Reply