<?php

namespace YSquare\Shipcloud\Core\Exception;

use Exception;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry;

class ShipcloudApiException extends StandardException
{
    /**
     * Exception constructor
     *
     * @param string $sMessage exception message
     * @param integer $iCode exception code
     * @param Exception|null $previous previous exception
     */
    public function __construct($sMessage = "not set", $iCode = 0, Exception $previous = null)
    {
        $sMessage = $this->getICodeMessage($iCode) . $sMessage;

        parent::__construct($sMessage, $iCode, $previous);
    }

    protected function getICodeMessage($iCode)
    {
        $oLang = Registry::getLang();

        if (in_array($iCode, [204, 400, 401, 402, 403, 404, 422, 500, 502, 504])) {
            return $oLang->translateString("YS_SHIPCLOUD_ERROR_" . $iCode);
        } else {
            return "[" . $iCode . "]";
        }
    }
}
