Skip to content
Snippets Groups Projects
Exception.php 3.84 KiB
<?php

namespace SmartData\Exception;

require_once( __DIR__ . '/HTTP.php');
require_once( __DIR__ . '/Logger.php');

use SmartData\{Config, Logger, HttpStatusCode};

interface IException {
    /* Protected methods inherited from Exception class */
    public function getMessage();// Exception message
    public function getCode();// User-defined Exception code
    public function getHTTPCodeError();
    public function getFile();// Source filename
    public function getLine();// Source line
    public function getTrace();// An array of the backtrace()
    public function getTraceAsString();// Formated string of trace
    /* Overrideable methods inherited from Exception class */
    public function __toString();// formated string for display
    public function __construct($message = null, $code = 0);
}

abstract class CustomException extends \Exception implements IException {
    protected $message = 'Unknown exception';// Exception message
    private $string;// Unknown
    protected $code = 0;// User-defined exception code
    protected $http = 400;// HTTP error code
    protected $file;// Source filename of exception
    protected $line;// Source line of exception
    private $trace;// Unknown

    public function __construct($message = null, $code = 0) {
        if (!$message) {
            throw new $this('Unknown '.get_class($this));
        }
        parent::__construct($message, $code);
    }

    public function __toString() {
        return get_class($this)."> {$this->file}:{$this->line} > {$this->message}\n";
    }

    public function getHTTPCodeError() {
        return $this->http;
    }
}

class BadRequestException         extends CustomException { protected $http = HttpStatusCode::BAD_REQUEST; }
class BadCredentialsException     extends BadRequestException {}

class UnauthorizedException       extends CustomException { protected $http = HttpStatusCode::UNAUTHORIZED; }
class NotImplementedException     extends CustomException { protected $http = HttpStatusCode::BAD_REQUEST; /*NOT_IMPLEMENTED;*/ }
class PreconditionFailed          extends CustomException { protected $http = HttpStatusCode::PRECONDITION_FAILED; }
class InternalException           extends CustomException { protected $http = HttpStatusCode::INTERNAL_SERVER_ERROR; }

class InvalidUnitException        extends BadRequestException {}
class AuthenticationException     extends UnauthorizedException {}

class NoMatchingSeriesException   extends BadRequestException {}
class NoMatchingTrackersException extends BadRequestException {}

class CreationFailedException     extends BadRequestException {}
class AttachmentFailedException   extends BadRequestException {}

class InsertionFailedException    extends BadRequestException {}
class RequestFailedException      extends BadRequestException {}

class CantCreateException         extends \Exception {}
// TODO: change this register shutdown function to better file
register_shutdown_function('SmartData\Exception\check4fatal');
function check4fatal() {
    $error = error_get_last();
    if (!is_null($error) && ($error["type"] == E_ERROR && Config::config()::FATAL_ERROR_REPORT)) {

        $signature = $error["type"].$error['file'].$error['line'];
        $signature = str_replace('/', "\\", $signature);
        $filename = Config::config()::FATAL_ERROR_DIR.$signature;

        if (!file_exists($filename)) {
            $error_log = "[".date("Y-m-d h:i:s")."] File: {$error['file']} ({$error['line']}) ";
            $lines     = explode(",", $error['message']);
            $error_log .= "Message: {$lines['0']}";

            $m = "Dear admin, a fatal error occurred on the IoT server.\n\n";
            $m .= $error_log."\n\n";
            $m .= "Best Regards,\n\nSV3";

            //mail(implode(", ",Config::config()::ADMIN_EMAIL), '[IoT/SV3] Fatal Error', $m);
            file_put_contents($filename, $m."\n", 0);
        }
    }

    Logger::flush();
}