Skip to content

Instantly share code, notes, and snippets.

@jptoto
Created February 1, 2012 12:42
Show Gist options
  • Save jptoto/1716843 to your computer and use it in GitHub Desktop.
Save jptoto/1716843 to your computer and use it in GitHub Desktop.
WebAPI Error Catch
[WebInvoke(UriTemplate = "", Method = "POST")]
public Email Post(Email email)
{
try
{
repository.Add(email);
foreach (var file in email.Attachments)
{
if (!string.IsNullOrEmpty(file.Content))
{
if (File.Exists(attachmentSaveFolder + file.Name))
File.Delete(attachmentSaveFolder + file.Name);
byte[] filebytes = Convert.FromBase64String(file.Content);
FileStream fs = new FileStream(attachmentSaveFolder + file.Name,
FileMode.CreateNew,
FileAccess.Write,
FileShare.None);
fs.Write(filebytes, 0, filebytes.Length);
fs.Close();
}
}
return email;
}
catch (HttpRequestException e)
{
// !!!
// HttpResponseExceptions are not properly returned yet. It's a known bug in WCF WebAPI preview 6. I'm hoping it's fixed in a subsequent release.
// !!!
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(e.InnerException.ToString()) });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment