Skip to main content

error_to_string

Function error_to_string 

Source
pub fn error_to_string(error: K) -> K
Expand description

Convert an error object into usual K object which has the error string in the field symbol.

§Example

use kdbplus::*;
use kdbplus::api::*;

extern "C" fn no_panick(func: K, args: K) -> K{
  let result=error_to_string(apply(func, args));
  if let Ok(error) = result.get_error_string(){
    println!("FYI: {}", error);
    // Decrement reference count of the error object which is no longer used.
    decrement_reference_count(result);
    KNULL
  }
  else{
    result
  }
}
q)chill: `libapi_examples 2: (`no_panick; 2);
q)chill[$; ("J"; "42")]
success!
42
q)chill[+; (1; `a)]
FYI: type

§Note

If you intend to use the error string only in Rust side and not to return the value, you need to decrement the reference count of the error object created by error_to_string as shown above. If you want to propagate the error to q side after some operation, you can just return it (See the example of is_error).

§Warning

In q, an error is a 0 pointer. This causes a problem of false positive by error_to_string, i.e., KNULL is also catched as an error object and its type is set qtype::ERROR. In such a case you must NOT return the catched object because it causes segmentation fault. If you want to check if the catched object is an error and then return if it is, you should use is_error. If you want to use the underlying error string of the catched object, you should use get_error_string.