GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Midgard Components Framework 3rd generation
Homepage: http://www.midgard-project.org
Clone URL: git://github.com/bergie/midcom.git
bergie (author)
Mon Sep 08 13:52:33 -0700 2008
commit  b3dd5cdf74983b51e761fad57cd745a7da112595
tree    aee90ed82bb40af71f0b221dde2be40333554c98
parent  ef58b84cc6f0da55c6d1ca6091fb1fdfd4e325ab
midcom / midcom_core / exceptionhandler.php
100644 147 lines (132 sloc) 4.181 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* @package midcom_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
 
/**
* MidCOM 3 exception handler
*
* @package midcom_core
*/
class midcom_core_exceptionhandler
{
    public static function handle(Exception $exception)
    {
        // Different HTTP error codes for different Exceptions
        $message_type = get_class($exception);
        switch ($message_type)
        {
            case 'midcom_exception_notfound':
            case 'midcom_exception_unauthorized':
            case 'midcom_exception_httperror':
                $http_code = $exception->getCode();
                break;
            default:
                $http_code = 500;
                break;
        }
 
        $message = $exception->getMessage();
        header("X-MidCOM-Error: {$message}");
 
        if (headers_sent())
        {
            die("<h1>Unexpected Error</h1>\n\n<p>Headers were sent so we don't have correct HTTP code ({$http_code}).</p>\n\n<p>{$message_type}: {$message}</p>\n");
        }
 
        $header = self::header_by_code($http_code);
 
        header($header);
        if ($http_code != 304)
        {
            header('Content-Type: text/html; charset=utf-8');
            
            $data['header'] = $header;
            $data['message_type'] = $message_type;
            $data['message'] = $message;
            $data['exception'] = $exception;
            
            try
            {
                if (!isset($_MIDCOM))
                {
                    throw new Exception("MidCOM not loaded");
                }
                $_MIDCOM->context->set_item('midcom_core_exceptionhandler', $data);
                $_MIDCOM->context->set_item('template_entry_point', 'midcom-show-error');
                
                $_MIDCOM->templating->template();
                $_MIDCOM->templating->display();
            }
            catch (Exception $e)
            {
                // Templating isn't working
                echo "<!DOCTYPE html>\n";
                echo "<html>\n";
                echo " <head>\n";
                echo " <title>{$header}</title>\n";
                echo " </head>\n";
                echo " <body class=\"{$message_type}\">\n";
                echo " <h1>{$header}</h1>\n";
                echo " <p>{$message}</p>\n";
                echo " </body>\n";
                echo "</html>";
            }
        }
    }
 
    private static function header_by_code($code)
    {
        $headers = array
        (
            200 => 'HTTP/1.0 200 OK',
            303 => 'HTTP/1.0 303 See Other',
            304 => 'HTTP/1.0 304 Not Modified',
            401 => 'HTTP/1.0 401 Unauthorized',
            404 => 'HTTP/1.0 404 Not Found',
            405 => 'HTTP/1.0 405 Method not allowed',
            500 => 'HTTP/1.0 500 Server Error',
            503 => 'HTTP/1.0 503 Service Unavailable',
        );
 
        if (!isset($headers[$code]))
        {
            $code = 500;
        }
 
        return $headers[$code];
    }
}
 
/**
* MidCOM 3 "not found" exception
*
* @package midcom_core
*/
class midcom_exception_notfound extends Exception
{
    // Redefine the exception so message isn't optional
    public function __construct($message, $code = 404)
    {
        parent::__construct($message, $code);
    }
}
 
/**
* MidCOM 3 "unauthorized" exception
*
* @package midcom_core
*/
class midcom_exception_unauthorized extends Exception
{
    // Redefine the exception so message isn't optional
    public function __construct($message, $code = 401)
    {
        parent::__construct($message, $code);
    }
}
 
/**
* MidCOM 3 generic HTTP error exception
*
* @package midcom_core
*/
class midcom_exception_httperror extends Exception
{
    // Redefine the exception so message isn't optional
    public function __construct($message, $code = 500)
    {
        parent::__construct($message, $code);
    }
}
 
set_exception_handler(array('midcom_core_exceptionhandler', 'handle'));
?>