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 8196245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:25:15+00:00 2026-06-07T05:25:15+00:00

I am trying to build an REST web service that returns JSON for all

  • 0

I am trying to build an REST web service that returns JSON for all calls. Normally this is pretty straightforward, I just do like this:

return new Response(json_encode($return_object));

My question is, how should I intercept exceptions in a global way? I want to do this, because if an exception happens anywhere in my application, I’d still like to return a JSON message to the client saying basically “Yo dawg, I heard you like exceptions”. My thinking is that returning JSON in both success and failure cases will simplify the work that any client needs to do to implement my API.

So far, the only thing I can think of is to write every controller action like this:

public function generateMemeAction($arg1, $arg2) {
  $return_object = array();
  try {
    // stuff to generate meme here
    $return_object['status'] = "GREAT SUCCESS!";
  } catch (Exception $e) {
    // epic fail
    $return_object['status'] = "UnluckyBrianException";
  }
  return new Response(json_encode($return_object));
}

Which is all great and wonderful, but that try-catch block will be the same for every action in my app, and I will feel silly every time I have to edit around a bunch of copy pasta. Pro tips?

  • 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-07T05:25:17+00:00Added an answer on June 7, 2026 at 5:25 am

    REST app have different type of errors. E.g. you can have logical errors or input errors (not all parameters were send). Different types of errors should be handling using diffent ways. The best way for that case is manual handling of these errors.

    You can add to your Controller special error method (and put it e.g. into parent class) which will return error code + error text.

    But if you want to use automatic handling you can use Exception listener:

    • http://api.symfony.com/2.0/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.html
    • http://api.symfony.com/2.7/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.html
    • http://symfony.com/doc/current/book/internals.html

    Here is a sample:

    use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
    use Symfony\Component\HttpFoundation\Response;
    
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $response = new Response();
        // setup the Response object based on the caught exception
        $event->setResponse($response);
    
        // you can alternatively set a new Exception
        // $exception = new \Exception('Some special exception');
        // $event->setException($exception);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to debug a rest/json service that i build using Jersey and Amazon
I am trying to build a REST & json based WCF service that takes
I'm attempting to build a REST based web service that provides a currency conversion
I'm trying to build a Rest JSON service with the WCF REST Service Template
Hi I'm trying to build up a rest Service which provide JSON response. Yesterday
I am currently trying to create a REST web service for my django site
I'm trying to build a JAX-RS based web service using IBM RAD 7.5.5 (which
im trying to build a application or a method that goes through all of
I am trying to build an interface that communicates with a REST API for
I'm trying to build a REST service in a Sitecore root. My application start

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.