<?php

namespace CouponsPlus\App\Coupon\Tree\Factories;

use CouponsPlus\App\Coupon\Tree\Abilities\CreatableFromJSON;
use CouponsPlus\App\Coupon\Tree\Abilities\ErrorHandler;
use Throwable;

class OnErrorJSONFactory extends FromJSONFactoryComposite implements CreatableFromJSON
{
    protected $errorHandler;

    public function setErrorHandler(ErrorHandler $errorHandler) : void
    {
        $this->errorHandler = $errorHandler;
    }

    public function create(string $json)
    {
        try {
            return $this->creatableFromJSON->create($json);
        } catch (Throwable $exception) {
            return $this->errorHandler->onError($exception);
        }
    } 
}