<commit_before><?php

namespace PhalconRest\Http;


class Response extends \Phalcon\Http\Response
{
    protected $defaultErrorMessages = [];

    public function getDefaultErrorMessages()
    {
        return $this->defaultErrorMessages;
    }

    public function setDefaultErrorMessages($messages)
    {
        $this->defaultErrorMessages = $messages;
    }

    public function setErrorContent(\Exception $e, $developerInfo=false)
    {
        $errorCode = $e->getCode();
        $statusCode = 500;
        $message = 'Unspecified error';

        if(array_key_exists($errorCode, $this->defaultErrorMessages)){

            $defaultMessage = $this->defaultErrorMessages[$errorCode];

            $statusCode = $defaultMessage['statuscode'];
            $message = $defaultMessage['message'];
        }

        $error = [
            'code' => $errorCode,
            'status' => $statusCode,
            'message' => $message,
        ];

        if ($developerInfo === true) {

            $error['developer'] = [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
                'message' => $e->getMessage(),
            ];
        }

        $this->setJsonContent(['error' => $error]);
    }

    public function setJsonContent($content, $jsonOptions = 0, $depth = 512)
    {
        parent::setJsonContent($content, $jsonOptions, $depth);

        $this->setHeader('Access-Control-Allow-Origin', '*');
        $this->setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
        $this->setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization');<commit_msg>Response sets status code on error<commit_after><?php

namespace PhalconRest\Http;


class Response extends \Phalcon\Http\Response
{
    protected $defaultErrorMessages = [];

    public function getDefaultErrorMessages()
    {
        return $this->defaultErrorMessages;
    }

    public function setDefaultErrorMessages($messages)
    {
        $this->defaultErrorMessages = $messages;
    }

    public function setErrorContent(\Exception $e, $developerInfo=false)
    {
        $errorCode = $e->getCode();
        $statusCode = 500;
        $message = 'Unspecified error';

        if(array_key_exists($errorCode, $this->defaultErrorMessages)){

            $defaultMessage = $this->defaultErrorMessages[$errorCode];

            $statusCode = $defaultMessage['statuscode'];
            $message = $defaultMessage['message'];
        }

        $error = [
            'code' => $errorCode,
            'status' => $statusCode,
            'message' => $message,
        ];

        if ($developerInfo === true) {

            $error['developer'] = [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
                'message' => $e->getMessage(),
            ];
        }

        $this->setJsonContent(['error' => $error]);
        $this->setStatusCode($statusCode);
    }

    public function setJsonContent($content, $jsonOptions = 0, $depth = 512)
    {
        parent::setJsonContent($content, $jsonOptions, $depth);

        $this->setHeader('Access-Control-Allow-Origin', '*');
        $this->setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
        $this->setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization');
{'fraction_non_alphanumeric': 0.15136255924170616, 'fraction_numerical': 0.004146919431279621, 'mean_word_length': 2.4958592132505175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3810484', 'n_tokens_mistral': 967, 'n_tokens_neox': 919, 'n_words': 155}