Article
· Apr 25 3m read

Making A Variable Watch Itself

I came up with a challenge for myself to come up with a way to make a variable watch itself for a certain value and do something when it hits that value without having to check it every time something touches it. Basically, a way to say "if at any point during the execution of this code, if x = 0 (or whatever the condition is) do this thing." The class I ended up with watches a %Status:

Class User.WatchedStatus Extends %RegisteredObject
{
Property sc As %Status [ InitialExpression = 1, SqlComputeCode = {set {*} = ##class(User.WatchedStatus).Reset({sc},{resetSelf})}, SqlComputed, SqlComputeOnChange = isErr ];
Property isErr As %Boolean [ InitialExpression = 0, SqlComputeCode = {set {*} = ##class(User.WatchedStatus).ErrCheck({sc},{writeSelf},{logSelf},{throwSelf})}, SqlComputed, SqlComputeOnChange = sc ];
Property throwSelf As %Boolean [ InitialExpression = 0 ];
Property logSelf As %Boolean [ InitialExpression = 0 ];
Property writeSelf As %Boolean [ InitialExpression = 0 ];
Property resetSelf As %Boolean [ InitialExpression = 0 ];

/// Handles status according to flags set, then sets isErr.
ClassMethod ErrCheck(sc, writeSelf, logSelf, throwSelf) As %Boolean [ Internal ]
{
	if $$$ISERR(sc){
		if writeSelf{
			write $SYSTEM.Status.GetErrorText(sc)
		}
		if logSelf = 1{
			do ##class(%Exception.StatusException).CreateFromStatus(sc).Log()
		}
		if throwSelf = 1{
			$$$ThrowStatus(sc)
		}
		quit 1
	}
	else{
		quit 0
	}
}

/// If resetSelf is true, resets the status code after error handling occurs.
ClassMethod Reset(sc, resetSelf) As %Status [ Internal ]
{
	return:resetSelf $$$OK
	return sc
}

/// flags is a string which determines status behavior when an error occurs
/// T = throw the status
/// L = log the status as an exception
/// W = write the status error text
/// R = reset status after error handling; if set, isErr goes back to 0 and sc goes back to 1
ClassMethod New(flags As %String) As User.WatchedStatus
{
	set status = ##class(User.WatchedStatus).%New()
	set flags = $ZCVT(flags,"U")
	set:(flags [ "T") status.throwSelf = 1
	set:(flags [ "L") status.logSelf = 1
	set:(flags [ "W") status.writeSelf = 1
	set:(flags [ "R") status.resetSelf = 1
	return status
}

}

If I create a new instance of this class using set status = ##class(User.WatchedStatus).New("wr"), then when I call methods that return a %Status, I can use set status.sc = (whatever method here) and if it returns an error status, it will write that status without me having to check if it's an error and tell it to do that every time, then reset itself for its next use. I also have a t or an l flag for if I want the status to throw itself or log itself as an exception in the system logs. (Note that if I didn't include the r flag, it wouldn't reset itself so that you could still check out the status code afterward.)

This uses SqlComputeCode in a way that it probably isn't meant to be used, and you have to be careful writing it this way. Since isErr computes itself on change of sc and sc computes itself on change of isErr, you could get yourself into a loop. That won't happen here because the the whole system will eventually settle again.

I'm not sure how much mileage everyone else will get out of this %Status example, but I'm documenting how I did it here anyway in case anyone else is ever trying to figure this out for a different kind of object.

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