Announcement

Collapse
No announcement yet.

How to catch an error from a plugin? (luacom)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to catch an error from a plugin? (luacom)

    Hi,

    I'm using luacom plugin for HTTP requests to a server (I need to personalize the headers).

    I get an error when I'm trying to send my request and the server is not available at that moment
    I don't know how to treat this plugin error.

    My function:
    Code:
    function HttpRequest(protocol, route, headers, data)
       [...]
       local httpReq = luacom.CreateObject("WinHttp.WinHttpRequest.5.1");
       httpReq:Open(protocol, URL .. route, false);
       [ ... set headers ... ]
       local jdata = JSON.Encode(data);
       httpReq:Send(jdata);
    
       if (httpReq.Status == 200) then
            local result = httpReq.ResponseText;
            return result;
       end
       [...]
    end
    Error is attached as image

    Line 72 is:
    Code:
     httpReq:Send(jdata);
    I would need some suggestion on how to get rid of this error

    Thank you in advance.

  • #2
    Have you included the luacom in the first place, doing luacon,create does not request the lib, also what you trying to do, as you can do the same thing with wget or ams's built in http, I myself use wget as the return comes as a file what's fine the others do also but they handles else where with wget you can handle where it stores it so it wont be in the computer known temp or history sections.


    But that error is telling you it as no idea what your asking of it, the I think you might have not requested to lib right.
    Plugins or Sources MokoX
    BunnyHop 2021 Here

    Comment


    • #3
      I included the luacom lib. The application is working fine. The requests are sending to the server.
      The problem appears when the server becomes unavailable and the application sends a request. In that moment I get that error.

      The reason for using the luacom object "WinHttp.WinHttpRequest.5.1", is because I need to set headers for requests.

      For example:

      Code:
      httpReq:SetRequestHeader("X-User", current_user);
      httpReq:SetRequestHeader("Content-Type", "application/json");
      Do you have any suggestions?

      Thank you!

      Comment


      • #4
        Explore the core lua function: pcall and or xpcall both designed to catch errors, you decide via code what to do with any "catchable" error returned by these lua functions

        Comment


        • #5
          https://www.lua.org/pil/8.4.html - information on how to catch an error in Lua. With pcall, the first result will be a boolean indicating success, the error message will be the second return value.

          As Eagle pointed out!
          Bas Groothedde
          Imagine Programming :: Blog

          AMS8 Plugins
          IMXLH Compiler

          Comment


          • #6
            Hello,

            I tried your suggestions but I couldn't suppress the error received from
            Code:
            httpReq:Send(jdata);
            I sent this question to tech support and I received an answer from reteset.
            He told me that
            there is a built-in luacom feature to disable errors with luacom plugin, setting following fields as false will disable error reports

            Code:
            luacom.config.abort_on_error = false;
            luacom.config.abort_on_API_error = false;
            and following field will contain a user friendly error message

            Code:
            luacom.config.last_error
            a complete example as follows

            Code:
            local WinHttp = luacom.CreateObject("WinHttp.WinHttpRequest.5.1","inproc_server",false);
            if (WinHttp ~= nil) then
                
                luacom.config.abort_on_error = false;
                luacom.config.abort_on_API_error = false;
            
                WinHttp:Open("POST", targetURL, false )
                WinHttp:SetRequestHeader("Host", "www.google.net")
                WinHttp:SetRequestHeader("Content-Type", "text/html")
                WinHttp:Send("");
                if(WinHttp.Status == nil) then
                    Dialog.Message("Error", "Failed To Complete WinHttp Request.\r\n\r\nReason : "..luacom.config.last_error);
                elseif (WinHttp.Status == 200) then
                    local result = WinHttp.ResponseText
                    Dialog.Message("Success", result);
                else
                    Dialog.Message("Error", "Failed To Complete WinHttp Request \r\n\r\n"..luacom.config.last_error.."\r\n\r\nHTTP STATUS : "..WinHttp.Status);
                end
                
                WinHttp = nil;
                collectgarbage();
            end
            in addition you can use WinHttp:SetTimeouts method to increase time out values ,see details below
            https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
            you can also see other properties and methods of httpobject from the link below
            https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
            You can find more details in the LuaCOM manual at page 23: http://files.luaforge.net/releases/l...om-1.4-doc.pdf , in the Exception Handling section.

            Thank you again, reteset.

            Comment

            Working...
            X