/// <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 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); }
protected T CreateFaultResult <T>(Exception exception, Func <int, VfsFault, T> func) where T : OperationResult { //set content type //Response.Headers.ContentType = new MediaType(VfsHttpHeaders.Default.VfsFaultContentType); //get exception VfsException ve = exception as VfsException; VfsFault fault = ve == null ? new VfsFault { Message = exception.Message } : VfsFault.CreateFromException(ve); int statusCode = 0; //set HTTP status code switch (fault.FaultType) { case VfsFaultType.ResourceNotFound: case VfsFaultType.TransferUnknown: //404 return(func(404, fault)); case VfsFaultType.ResourceAccess: case VfsFaultType.ResourceOverwrite: case VfsFaultType.ResourceLocked: case VfsFaultType.TransferError: case VfsFaultType.TransferStatusError: case VfsFaultType.DataBlockError: case VfsFaultType.ResourcePathInvalid: //forbidden return(func(403, fault)); case VfsFaultType.Undefined: return(func(500, fault)); default: string msg = String.Format("Exception contains unknown fault type [{0}]", fault.FaultType); throw new ArgumentOutOfRangeException("exception", msg); } }