Question
· Jun 2, 2017

Issue with error-text of custom error-code

Hi,

I am trying to generate a custom error-code using the following code

Class ISG.CommonBilling.Service.Test1 Extends EnsLib.HL7.Operation.TCPOperation
{
    ClassMethod khalid() As %Status
    {
        Set tSC=$$$ERROR("10001","I am here")
        write $$$GETERRORCODE(tSC)_$char(13,10)
        write $SYSTEM.Status.GetErrorText(tSC)
    }
}

And here is the output:

KINDRED>do ##class(ISG.CommonBilling.Service.Test1).khalid()
10001
ERROR #10001: Unknown status code: 10001 (I am here)

QUERY: Is there any way I can remove the part "Unknown status code: 10001 (...)" in the error-text?

Thanks in advance,
Khalid

Discussion (5)0
Log in or sign up to continue

Try generating an error with code 5001 ($$$GeneralError, "your custom message").
This will give you an "#ERROR 5001: your custom message",  $replace that "#ERROR 5001:" part to "" using GetErrorText.

Note that if you have multiple errors inside a single status you'll need to fetch and $replace them individually.

That's because I don't think you can generate custom error codes. I would delegate the errors to your application layer instead.

As I wanted to get the custom error-code along with the custom error-text, I used the regex-class "%Regex.Matcher" to pull the content/custom-text within the parenthesis in the error-text.  This served my purpose.  Thanks for looking into it... :-)

Here is the modified code:

Class ISG.CommonBilling.Service.Test1 Extends EnsLib.HL7.Operation.TCPOperation
{

ClassMethod khalid() As %Status
{
Set tSC=$$$ERROR("10001","I am here")
write $$$GETERRORCODE(tSC)_$char(13,10)
write ##class(%Regex.Matcher).%New(".*\((.*)\).*",$SYSTEM.Status.GetErrorText(tSC)).ReplaceAll("$1")
}

}

 

Output:

KINDRED>do ##class(ISG.CommonBilling.Service.Test1).khalid()
10001
I am here

Here's how you can define your own errors.

Create messages.xml with your error messages:

<?xml version="1.0" encoding="UTF-8"?>
<MsgFile Language="en">
    <MsgDomain Domain="asd">
        <Message Id="-1" Name="ErrorName1">Your error 1</Message>
        <Message Id="-2" Name="ErrorName2">Some other error 2 %1 %2</Message>
    </MsgDomain>
</MsgFile>

Import it into Cache:

do ##class(%MessageDictionary).Import("messages.xml")

It will result in two globals being populated:

  • ^CacheMsg
USER>zw ^CacheMsg 
^CacheMsg("asd","ru",-2)="Some other error 2" 
^CacheMsg("asd","ru",-1)="Your error 1"
  •   ^CacheMsgNames
USER>zw ^CacheMsgNames 
^CacheMsgNames("asd",-2)="ErrorName2" 
^CacheMsgNames("asd",-1)="ErrorName1"

Next we generate CustomErrors include file (docs):

USER>Do ##class(%MessageDictionary).GenerateInclude("CustomErrors",,"asd",1) 
Generating CustomErrors.INC ...

Here's what CustomErrors.inc looks like:

#define asdErrorName2 "<asd>-2"
#define asdErrorName1 "<asd>-1"

Now we can use new custom errors:

Include CustomErrors

Class demo.test [ Abstract ]
{

ClassMethod test(A As %Integer) As %Status
{
  if A=1 Quit $$$ERROR($$$asdErrorName1)
  if A=2 Quit $$$ERROR($$$asdErrorName2,"f","6")
  Quit $$$OK
}

}

Results in:

USER>d $system.OBJ.DisplayError(##class(demo.test).test(1))

ERROR <asd>-1: Your error 1

USER>d $system.OBJ.DisplayError(##class(demo.test).test(2))

ERROR <asd>-2: Some other error 2 f 6

Text is not mine, translated from here.