<?php

namespace AuthStack\UserDirectory\Exceptions;

use Illuminate\Http\JsonResponse;

class UserDirectoryException extends \Exception
{
    public function __construct(string $message, int $code = 0, $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    public function render(): JsonResponse
    {
        return response()->json(['error' => [$this->getMessage()]], $this->getHttpStatusCode());
    }

    protected function getHttpStatusCode(): int
    {
        if(empty($this->getCode()) || 0 === $this->getCode())
        {
            return 422;
        }

        return $this->getCode();
    }
}