# Problem with log message when using trap

**URL:** <https://forum.kx.com/t/problem-with-log-message-when-using-trap/12255>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 10, 2021, 8:17pm UTC](https://forum.kx.com/t/problem-with-log-message-when-using-trap/12255 "2021-11-10T20:17:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ekallivrousis1](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@ekallivrousis1](https://forum.kx.com/u/ekallivrousis1)\
**Post date:** [November 10, 2021, 8:17pm UTC](https://forum.kx.com/t/problem-with-log-message-when-using-trap/12255/1 "2021-11-10T20:17:00Z")

</div>

closeHandle:{[h] .util.logMsg[“Closing handle”]; update handle:0Ni from `.cfg.handles where handle=h; @[hclose; h;.util.logError["Error closing handle: ",(string h)]]; }; logMsg:{[x] show string[.z.Z], "-" , x; }; logError:{ logMsg["ERROR: ",x]; :`error; };

For some reason the trap is printing error closing handle when infact its closing it. I only want it to print when it might have issue closing it. Am i missing something?

---

<div class="post-metadata">

**Author:** ![Jason\_Fealy1](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jason\_Fealy1](https://forum.kx.com/u/Jason_Fealy1)\
**Post date:** [November 10, 2021, 10:08pm UTC](https://forum.kx.com/t/problem-with-log-message-when-using-trap/12255/2 "2021-11-10T22:08:00Z")

</div>

Hi, the trap itself needs to be a function otherwise it will be evaluated regardless of whether the application of your function (hclose) fails or not.&nbsp;  
https://code.kx.com/q/ref/apply/#when-e-is-not-a-function&nbsp;

q)logError:{-1"Error: “,x;} q)@[10+;10;logError"Can’t add”] Error: Can’t add 20 q)@[10+;10;{logError"Can’t add"}] 20 q)@[10+;`abc;{logError"Can’t add"}] Error: Can’t add

&nbsp;

---

<div class="post-metadata">

**Author:** ![ekallivrousis1](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@ekallivrousis1](https://forum.kx.com/u/ekallivrousis1)\
**Post date:** [November 11, 2021, 5:13am UTC](https://forum.kx.com/t/problem-with-log-message-when-using-trap/12255/3 "2021-11-11T05:13:00Z")

</div>

This looks good but how can i pass a parameter to that function? doesnt seem to allow me to pass anything.

---

<div class="post-metadata">

**Author:** ![cillianreilly1](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cillianreilly1](https://forum.kx.com/u/cillianreilly1)\
**Post date:** [November 11, 2021, 5:29am UTC](https://forum.kx.com/t/problem-with-log-message-when-using-trap/12255/4 "2021-11-11T05:29:00Z")

</div>

The error thrown is passed as the first parameter to the function. https://code.kx.com/q/ref/apply/#trap. To allow parameters, you need to have a function of rank 2 or greater, you can then create a projection of a rank 1 function by eliding the first parameter:

&nbsp;

&nbsp;

q)logError:{-1"Error: ",x;} q)@[10+;`abc;{logError x,". Couldn't add 10 to ",string y}[;`abc]] Error: type. Couldn’t add 10 to abc

&nbsp;

&nbsp;

You can extend this to allow additional parameters to your error function by creating greater rank functions
