/// <summary> /// Validates an object for null with defered excecution of error message /// formating and default ErrReportException /// </summary> /// <param name="code">The error code if null</param> /// <param name="obj">The object to evaluate</param> /// <param name="msg">The error message function</param> public static void ChkVar(object obj, int code, Func <string> msgFunc) { if (obj == null) { WrapErr.ChkVar(obj, code, WrapErr.SafeAction(msgFunc)); } }
/// <summary> /// Validates a condition to be false with defered formating of error message /// with ErrReportException thrown on failure /// </summary> /// <param name="condition">The condition to evaluate</param> /// <param name="code">The error code if true</param> /// <param name="msg">The error message function</param> public static void ChkFalse(bool condition, int code, Func <string> msgFunc) { if (condition) { // Format method only invoked on failure of condition WrapErr.ChkFalse(condition, code, WrapErr.SafeAction(msgFunc)); } }
/// <summary> /// Log the ErrReport /// </summary> /// <param name="err"></param> private static ErrReport LogErr(ErrReport err) { lock (WrapErr.onExceptionLogLock) { if (WrapErr.onExceptionLog != null) { WrapErr.SafeAction(() => WrapErr.onExceptionLog.Invoke(err)); } } return(err); }
/// <summary> /// Wrapper of try catch finally to consitently produce a FaultException ErrReport no matter what the /// Exception. Information from previous FaultException ErrReport is just retthrown while /// ErrReportException are converted to FaultException ErrReport with embedded ErrReport info /// </summary> /// <param name="code">The unique error number</param> /// <param name="errMsgFunc">The error message formating method that is only invoked on exception</param> /// <param name="action">The action to execute</param> /// <param name="finallyAction">The finally action to execute</param> private static T WrapTryToErrorReportException <T>(int code, Func <string> errMsgFunc, Func <T> func, Action finallyAction) { try { return(func.Invoke()); } catch (ErrReportExceptionFromChk e) { throw new ErrReportException(WrapErr.GetErrReport(e)); } catch (ErrReportException) { throw; } catch (Exception e) { throw new ErrReportException(WrapErr.GetErrReport(code, WrapErr.SafeAction(errMsgFunc), e)); } finally { WrapErr.SafeAction(() => finallyAction.Invoke()); } }
/// <summary> /// Wrap an action and report the results with an ErrReport object. No exceptions will propegate /// and a finally will be executed. The error message is an anonymous method to prevent costly /// string formating when no error. /// </summary> /// <param name="report">The ErrReport object to initialise</param> /// <param name="code">The error code on error</param> /// <param name="errMsgFunc">The error message on error function</param> /// <param name="action">The action to invoke</param> /// <param name="finallyAction">The finally action to invoke</param> public static ErrReport WrapTryToErrReport(int code, Func <string> errMsgFunc, Action action, Action finallyAction) { try { action.Invoke(); return(new ErrReport()); } catch (ErrReportExceptionFromChk e) { return(WrapErr.GetErrReport(e)); } catch (ErrReportException e) { return(e.Report); } catch (Exception e) { return(WrapErr.GetErrReport(code, WrapErr.SafeAction(errMsgFunc), e)); } finally { WrapErr.SafeAction(() => finallyAction.Invoke()); } }
/// <summary> /// Wrap a function and report the results with an ErrReport object. No exceptions will propegate /// and a finally will be executed. The error message is an anonymous method to prevent costly /// string formating when no error. /// </summary> /// <param name="report">The ErrReport object to initialise</param> /// <param name="code">The error code on error</param> /// <param name="errMsgFunc">The error message on error function</param> /// <param name="func">The function to invoke</param> /// <param name="finallyAction">The finally action to invoke</param> public static T WrapTryToErrReport <T>(out ErrReport report, int code, Func <string> errMsgFunc, Func <T> func, Action finallyAction) { try { T ret = func.Invoke(); report = new ErrReport(); return(ret); } catch (ErrReportExceptionFromChk e) { report = WrapErr.GetErrReport(e); } catch (ErrReportException e) { report = e.Report; } catch (Exception e) { report = WrapErr.GetErrReport(code, WrapErr.SafeAction(errMsgFunc), e); } finally { WrapErr.SafeAction(() => finallyAction.Invoke()); } return(default(T)); }