Skip to content

Latest commit

 

History

History
106 lines (83 loc) · 1.95 KB

errors.md

File metadata and controls

106 lines (83 loc) · 1.95 KB

Errors

Json Responses displays an additional errors array if your exception implement M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\FlattenErrorExceptionInterface

Just create an exception with getFlattenErrors method to transform errors to array

<?php 

namespace Acme\DemoBundle\Exception;

use M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\FlattenErrorExceptionInterface;

/**
 * Class UnknownException
 */
class UnknownException implements FlattenErrorExceptionInterface
{
    /**
     * @var mixed
     */
    protected $var1;
    
    /**
     * @var mixed
     */
    protected $var2;
    
    /**
     * @param mixed $errors
     */
    public function __construct($var1, $var2)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
    }
    
    /**
     * Get errors
     *
     * @return array
     */
    public function getFlattenErrors()
    {
        $errors = [];
        
        /* your algo with $var1 and $var2 to compose array */
        
        return $errors
    }
}

Example: M6Web\Bundle\ApiExceptionBundle\Exception\ValidationFormException

Use your exception with errors

<?php 

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\DemoBundle\Exception\UnknownException;

/**
 * Class DefaultController
 */
class DefaultController extends Controller
{
    /*...*/

    throw new UnknownException($var1, $var2);
            
    /*...*/
}

result to json response

{
    "error": {
        "status": 400,
        "code": 1526,
        "message": "error during process",
        "errors": {
            "error1": [
                "This is first problem with error1",
                "This is second problem with error1"
            ],
            "error2": [
                "This is problem with error2"
            ],
    },
    "stack_trace": [
        ...
    ]
  }
}

Prev : Stack Trace

Next : Form