public void Already_Audited_Exceptions_Should_Not_Be_Audited_Anymore() { VfsException e = new ResourceAccessException("hello") {IsAudited = true}; AuditHelper.AuditException(auditor, e, FileSystemTask.RootFolderInfoRequest); Assert.AreEqual(0, auditor.Items.Count); }
/// <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; } }
public void Exceptions_With_Audit_Suppression_Should_Be_Ignored() { VfsException e = new ResourceAccessException("hello") { SuppressAuditing = true }; AuditHelper.AuditException(auditor, e, FileSystemTask.RootFolderInfoRequest); Assert.AreEqual(0, auditor.Items.Count); }
/// <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; } }
public void Submitted_Exceptions_Should_Indicate_They_Have_Been_Audited_After_Processing() { VfsException e = new ResourceAccessException("hello"); Assert.IsFalse(e.IsAudited); AuditHelper.AuditException(auditor,e, FileSystemTask.RootFolderInfoRequest); Assert.IsTrue(e.IsAudited); }
public void Submitted_Exceptions_Should_Be_Logged() { VfsException e = new ResourceAccessException("hello"); AuditHelper.AuditException(auditor, e, FileSystemTask.RootFolderInfoRequest); Assert.AreEqual(1, auditor.Items.Count); Assert.AreEqual(FileSystemTask.RootFolderInfoRequest, auditor.Items[0].Context); }
public void Submitted_Exceptions_Should_Indicate_They_Have_Been_Audited_After_Processing() { VfsException e = new ResourceAccessException("hello"); Assert.IsFalse(e.IsAudited); AuditHelper.AuditException(auditor, e, FileSystemTask.RootFolderInfoRequest); Assert.IsTrue(e.IsAudited); }
public void Already_Audited_Exceptions_Should_Not_Be_Audited_Anymore() { VfsException e = new ResourceAccessException("hello") { IsAudited = true }; AuditHelper.AuditException(auditor, e, FileSystemTask.RootFolderInfoRequest); Assert.AreEqual(0, auditor.Items.Count); }
public static VfsException ToException(this VfsFault fault) { VfsException exception; switch (fault.FaultType) { case VfsFaultType.ResourceNotFound: exception = new VirtualResourceNotFoundException(fault.CreateFaultMessage()); break; case VfsFaultType.ResourceAccess: exception = new ResourceAccessException(fault.CreateFaultMessage()); break; case VfsFaultType.ResourceOverwrite: exception = new ResourceOverwriteException(fault.CreateFaultMessage()); break; case VfsFaultType.ResourceLocked: exception = new ResourceLockedException(fault.CreateFaultMessage()); break; case VfsFaultType.ResourcePathInvalid: exception = new InvalidResourcePathException(fault.CreateFaultMessage()); break; case VfsFaultType.TransferError: exception = new TransferException(fault.CreateFaultMessage()); break; case VfsFaultType.TransferUnknown: exception = new UnknownTransferException(fault.CreateFaultMessage()); break; case VfsFaultType.TransferStatusError: exception = new TransferStatusException(fault.CreateFaultMessage()); break; case VfsFaultType.DataBlockError: exception = new DataBlockException(fault.CreateFaultMessage()); break; case VfsFaultType.Undefined: exception = new VfsFaultException(fault.CreateFaultMessage(), fault); break; default: Debug.WriteLine("Unsupported VFS fault type: " + fault.FaultType); exception = new VfsFaultException(fault.CreateFaultMessage(), fault); break; } exception.EventId = fault.EventId; return(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; } }
/// <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="func">The function 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="func"/> function.</returns> public static T SecureFunc <T>(FileSystemTask task, Func <T> func, Func <string> errorMessage, IAuditor auditor) { try { return(func()); } catch (VfsException e) { //just audit and rethrow VFS exceptions AuditHelper.AuditException(auditor, e, task); throw; } catch (Exception e) { //wrap unhandled exception into VFS exception var exception = new ResourceAccessException(errorMessage(), e); AuditHelper.AuditException(auditor, exception, task); throw exception; } }