Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:11:25+00:00 2026-06-13T23:11:25+00:00

When Tasks returned by (asynchronous) ASP.NET Web API controller methods throw exceptions, I would

  • 0

When Tasks returned by (asynchronous) ASP.NET Web API controller methods throw exceptions, I would like to translate known exceptions to HttpResponseException. Is there some way to intercept these exceptions, and throw HttpResponseException instead?

The following code snippet should demonstrate the type of asynchronous API controller method I’m talking about.

class ObjectApiController : ApiController
{
    public Task<Object> GetObjectByIdAsync(string id)
    [...]
}

Let me know if I need to provide more information about what I’m trying to do.

EDIT:
Specifically I’d like to know if there’s some kind of hook with which I can intercept exceptions from Tasks returned by ApiController methods, and translate said exceptions to HttpResponseException.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:11:25+00:00Added an answer on June 13, 2026 at 11:11 pm

    I use an ExceptionFilter to do this:

    /// <summary>
    /// Formats uncaught exception in a common way, including preserving requested Content-Type
    /// </summary>
    public class FormatExceptionsFilterAttribute : ExceptionFilterAttribute
    {
    
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            Exception exception = actionExecutedContext.Exception;
    
            if (exception != null)
            {            
                HttpRequestMessage request = actionExecutedContext.Request;
    
                // we shouldn't be getting unhandled exceptions
                string msg = "Uncaught exception while processing request {0}: {1}";
                AspLog.Error(msg.Fmt(request.GetCorrelationId().ToString("N"), exception), this); 
    
                // common errror format, without sending stack dump to the client
                HttpError error = new HttpError(exception.Message);
                HttpResponseMessage newResponse = request.CreateErrorResponse(
                    HttpStatusCode.InternalServerError,
                    error);
                actionExecutedContext.Response = newResponse; 
    
            }
        }
    
    }
    

    It is registered like this:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Filters.Add(new ApiControllers.FormatExceptionsFilterAttribute());
    
            // other stuff
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ADAM set-up & I've written web-services to complete admin tasks like adding
I'm using Tasks in WinForms to remove expensive methods from my UI thread. In
I've created a wrapper around boost::asio::io_service to handle asynchronous tasks on the GUI thread
I would like put the code first and then explain the situation and ask
I have an object Tasks with a model like this has_many :notes, :validate =>
I thought TaskEx.WhenAll would return when all the tasks gets finished which is passed
I would like to use HttpClient to read the chunked (in the sense of
I am using the Google Tasks API to return values, such as task title,
In my WPF Window_Loaded event handler I have something like this: System.Threading.Tasks.Task.Factory.StartNew(() => {
I am using Quartz.NET for scheduling some custom tasks in our application. Everything works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.