StLia
StLia

Reputation: 1072

force ajax to return 200 on error

I want to hide the exceptions thrown in the console so I want to hard-code the status code to 200 for all exceptions. In my case, I want to change a 500 internal server error response.

I know it's a bad practise but there are cases that can be used..

Upvotes: 0

Views: 97

Answers (1)

zessx
zessx

Reputation: 68820

You'll need to use a try/catch. Here's a dummy example:

try {

  return json_encode(array(
    'code' => '200',
    'result' => array()
  ));

} catch (MissingArgumentException $e) {

  // Known exception
  return json_encode(array(
    'code' => '400',
    'message' => 'Bad request: missing argument.',
    'result' => null
  ));

} catch (\Exception $e) {

  // Default exception
  return json_encode(array(
    'code' => '500',
    'message' => sprintf('An error occured: %s', $e->getMessage()),
    'result' => null
  ));

}

Upvotes: 4

Related Questions