Search code examples
kdb

.Q.hp giving error when used in try-catch


When tried to run .Q.hp (https://code.kx.com/q/ref/dotq/#hp-http-post) in try-catch, I am getting an error ".[k){hmb[x;`POST;(y;z)]};[Ljava.lang.Object;@7c8be683;[Ljava.lang.Object;@724c1762;]"

I am not sure what I am doing wrong here and how to fix it, any help would be appreciated

//defaine args
args:`channel`fallback`pretext`color`title`messageValue!("qatesting";"qatesting";"";"";(string .proc.hostip)," This is a test run, plz ignore";"");

//works fine
.Q.hp[webhookKey;"application/json";(.j.j raze (enlist (`channel`link_names`attachments)!(args[`channel];1b;((enlist (`fallback`pretext`color`fields)!(args[`fallback];args[`pretext];args[`color];((enlist (`title`value`short)!(args[`title];args[`messageValue];0b)))))))))]

//getting error
.[.Q.hp;(webhookKey;"application/json";(.j.j raze (enlist (`channel`link_names`attachments)!(args[`channel];1b;((enlist (`fallback`pretext`color`fields)!(args[`fallback];args[`pretext];args[`color];((enlist (`title`value`short)!(args[`title];args[`messageValue];0b))))))))));.lg.e[`slack_post_msg;"We have got an error when doing .Q.hp in .slack.post_msg"];]
".[k){hmb[x;`POST;(y;z)]};[Ljava.lang.Object;@7c8be683;[Ljava.lang.Object;@724c1762;]"


Solution

  • Matt's comment are correct. The main issues are the extra ; in your .[;;] trap and the lack of a wrapper for the error handler to delay it's execution.

    Breaking out the code more and using named variables can help you spot syntax errors more easily:

    //A nice way to define dictionaries in a readable format
    args:(!). flip (
     (`channel     ;"qatesting");
     (`fallback    ;"qatesting");
     (`pretext     ;"");
     (`color       ;"");
     (`title       ;"233312 This is a test run, plz ignore");
     (`messageValue;"")
     );
    
    formatargs:(webhookKey;
          "application/json";
          (.j.j raze (enlist (`channel`link_names`attachments)!(args[`channel];
           1b;
           ((enlist (`fallback`pretext`color`fields)!(
              args[`fallback];
              args[`pretext];
              args[`color];
              ((enlist (`title`value`short)! 
                 (args[`title];args[`messageValue];0b))))))))))
    
    errHandler:{[err]
     .lg.e[`slack_post_msg;
           "We have got an error when doing .Q.hp in .slack.post_msg: ",err]
     }
    
    .[.Q.hp;formatargs;errHandler]
    

    I made the addition of : ",err so that the specific error thrown can be included in the error log which will help with debugging.