addCause method
Returns a UserException from the current one, by adding the given cause
.
Note the added cause
won't replace the original cause, but will be added to it.
If the added cause
is a null
, it will return the current exception, unchanged.
If the added cause
is a String, the addReason method will be used to
return the new exception.
If the added cause
is a UserException, the mergedWith method will be used to
return the new exception.
If the added cause
is any other type, including any other error types, it will be
set as the property hardCause of the exception. The hard cause is meant to be some
error which caused the UserException, but that is not a UserException itself.
For example, if int.parse('a')
throws a FormatException
, then
throw UserException('Invalid number').addCause(FormatException('Invalid input'))
.
will set the FormatException
as the hard cause.
Implementation
@useResult
UserException addCause(Object? cause) {
//
if (cause == null) {
return this;
}
//
else if (cause is String) {
return addReason(cause);
}
//
else if (cause is UserException) {
return mergedWith(cause);
}
//
// Now we're going to set the hard cause.
else {
return AdvancedUserException(
message,
reason: reason,
code: code,
onOk: onOk,
onCancel: onCancel,
props: props,
errorText: errorText,
ifOpenDialog: ifOpenDialog,
hardCause: cause, // We discard the old hard cause, if any.
);
}
}