Created
May 10, 2014 03:14
-
-
Save mgroves/b21f8e1afa7ac43e4145 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Serializable] | |
| [MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Public)] | |
| public class ServiceErrorInterceptor : MethodInterceptionAspect | |
| { | |
| public override bool CompileTimeValidate(MethodBase method) | |
| { | |
| if (!typeof (ServiceBase).IsAssignableFrom(method.DeclaringType)) | |
| { | |
| var declaringType = method.DeclaringType; | |
| var declaringTypeName = "Unknown"; | |
| if (declaringType != null) | |
| declaringTypeName = declaringType.FullName; | |
| Message.Write(method, SeverityType.Error, "SEI01", "The target type (" + declaringTypeName + ") does not implement ServiceBase."); | |
| return false; | |
| } | |
| return base.CompileTimeValidate(method); | |
| } | |
| public override void OnInvoke(MethodInterceptionArgs args) | |
| { | |
| try | |
| { | |
| args.Proceed(); | |
| } | |
| catch (SqlException ex) | |
| { | |
| var obj = (ServiceBase) args.Instance; | |
| if (ex.Message.Contains("The DELETE statement conflicted with the REFERENCE constraint")) | |
| obj.AddValidationError("This item can't be deleted because it is still used by other items."); | |
| else | |
| { | |
| obj.AddValidationError(GetErrorMessage(ex)); | |
| if (!IsThisSite.RunningLocally) | |
| ex.ToExceptionless().Submit(); | |
| } | |
| ErrorReturn(args); | |
| } | |
| catch (NotImplementedException) | |
| { | |
| // if something's not implemented, just throw it up | |
| throw; | |
| } | |
| catch (Exception ex) | |
| { | |
| if (!IsThisSite.RunningLocally) | |
| ex.ToExceptionless().Submit(); | |
| var obj = (ServiceBase)args.Instance; | |
| obj.AddValidationError(GetErrorMessage(ex)); | |
| ErrorReturn(args); | |
| } | |
| } | |
| private string GetErrorMessage(Exception ex) | |
| { | |
| if(IsThisSite.RunningLocally) | |
| return "There was a database error. Please try again later. (" + ex.Message + "), (" + ex.GetType().FullName + ")"; | |
| return "There was a database error. Please try again later."; | |
| } | |
| void ErrorReturn(MethodInterceptionArgs args) | |
| { | |
| try | |
| { | |
| var methodInfo = args.Method as MethodInfo; | |
| if (methodInfo == null) | |
| return; | |
| if (typeof (IEnumerable).IsAssignableFrom(methodInfo.ReturnType) || methodInfo.ReturnType.IsValueType) | |
| if (methodInfo.ReturnType != typeof (void)) | |
| args.ReturnValue = Activator.CreateInstance(methodInfo.ReturnType); | |
| else | |
| args.ReturnValue = null; | |
| } | |
| catch | |
| { | |
| // don't want any errors to take place trying to build the correct return value | |
| // so just swallow them, and let args.ReturnValue stay whatever it is | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment