Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mgroves/5f907e1b0ab273309433 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/5f907e1b0ab273309433 to your computer and use it in GitHub Desktop.
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 an error. Please try again later. (" + ex.Message + "), (" + ex.GetType().FullName + ")";
return "There was an error. Please try again later.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment