Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

365
Views
Using json's value as slim's error code

Right now I have this:

$error = new Error($id, $from, $to);
return $response
    ->withStatus(422)
    ->withHeader('Content-Type', 'text/html')
    ->write($error->error());

The Error class returns this:

{"error":true,"code":422,"text":"Blah!"}

How can I set the error code in ->withStatus(422) so it's set by the json string instead of by me manually?

Edit: I guess this could work:

$code = json_decode($error->error(), true);
$code = $code['code'];
/* ...blah blah code... */
->withStatus($code)

But I would like to know if there's a way to do it with less code.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Update the Error class so that it has a code() public method that returns just the 422. You can now do:

$error = new Error($id, $from, $to);
return $response
    ->withStatus($error->code())
    ->withHeader('Content-Type', 'application/json')
    ->write($error->error());

Incidentally, as you're using JSON, if you make error() return an array, you can do this:

$error = new Error($id, $from, $to);
return $response->withJson($error->error(), $error->code());
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!