Question

function put($id, $name)
{
   try
   {
       product::put($id, $name);
   }
   catch(\util\BadNameException $e)
   {
       throw new RestException(400, "Please supply a better name.");
   }
}

When returning the error message I also want to include the result of (array)product::getNamingConvention() in the error. How can I do that?

I could just return a custom array with the error message and data, but I don't know how to set the status code to 400 in that case?

I'm using Restler 3.

Was it helpful?

Solution

Responder class is responsible for giving a structure to error response and success response

You can extend the Responder class as shown below to add data property

use \Luracast\Restler\Responder;
use \Luracast\Restler\Defaults;

class MyResponder extends Responder
{
    public static $data = null;

    public function formatError($statusCode, $message)
    {
        $r = array(
            'error' => array(
                'code' => $statusCode,
                'message' => $message
            )
        );
        if (isset(self::$data)) {
            $r['data'] = self::$data;
        }
        return $r;
    }
}

Defaults::$responderClass = 'MyResponder';

And then set the data from your class as shown below

function put($id, $name)
{
   try
   {
       product::put($id, $name);
   }
   catch(\util\BadNameException $e)
   {
       MyResponder::$data = product::getNamingConvention();
       throw new RestException(400, "Please supply a better name.");
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top