/// <summary> /// Invokes a given function and provides error handling and auditing in case the method causes /// an exception. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="auditor"></param> /// <param name="context"></param> /// <param name="func"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static T SecureFunc <T>(IAuditor auditor, FileSystemTask context, Func <T> func, Func <string> errorMessage) { try { return(func()); } // catch (FaultException<ResourceFault> e) // { // throw; //TODO provide implementation // } // catch (FaultException e) // { // //unwrap exception details // throw; //TODO provide implementation // } // catch (CommunicationException e) // { // throw; //TODO provide implementation // } catch (VfsException e) { //just audit and rethrow VFS exceptions auditor.AuditException(e, context); throw; } catch (Exception e) { //wrap unhandled exception into VFS exception var exception = new ResourceAccessException(errorMessage(), e); auditor.AuditException(exception, context); throw exception; } }
/// <summary> /// Invokes a given function and provides error handling and auditing in case the method causes /// an exception. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="auditor"></param> /// <param name="context"></param> /// <param name="func"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static T SecureFunc <T>(IAuditor auditor, FileSystemTask context, Func <T> func, Func <string> errorMessage) { try { return(func()); } catch (VfsFaultException e) { //audit as warning - the error has been properly handled by the service VfsFault fault = e.Fault; auditor.AuditException(e, AuditLevel.Warning, context, AuditEvent.Unknown, fault.CreateFaultMessage()); //create a matching exception based on the fault type throw fault.ToException(); } catch (VfsException e) { //just audit and rethrow VFS exceptions auditor.AuditException(e, context); throw; } catch (Exception e) { //wrap unhandled exception into VFS exception var exception = new ResourceAccessException(errorMessage(), e); auditor.AuditException(exception, context); throw exception; } }
/// <summary> /// Provides exception handling and auditing for a given function. /// </summary> /// <param name="task">The context, used for auditing exceptions that may occur.</param> /// <param name="action">The action to be invoked.</param> /// <param name="errorMessage">Returns an error message in case of an unhandled exception /// that is not derived from <see cref="VfsException"/>.</param> /// <param name="auditor">The auditor that receives the exceptions.</param> /// <returns>The result of the submitted <paramref name="action"/> function.</returns> public static void SecureAction(FileSystemTask task, Action action, Func <string> errorMessage, IAuditor auditor) { try { action(); } catch (VfsException e) { //just audit and rethrow VFS exceptions auditor.AuditException(e, task); throw; } catch (Exception e) { //wrap unhandled exception into VFS exception var exception = new ResourceAccessException(errorMessage(), e); auditor.AuditException(exception, task); throw exception; } }