-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.cb_api_exception.php
70 lines (64 loc) · 2.46 KB
/
class.cb_api_exception.php
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
<?php
/* This file is part of cbapi.
* Copyright © 2011-2012 stiftung kulturserver.de ggmbh <[email protected]>
*
* cbapi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbapi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbapi. If not, see <http://www.gnu.org/licenses/>.
*/
Cb::import("CbHttpResponseCodes");
/**
* Class to provide exceptions mappable to HTTP response codes. Usually this
* will be thrown from a utility class and caught by CbContentProvider. For
* now we can only transfer a single string message. Subclass this and provide
* a more sophisticated getMessage to get something else.
*/
class CbApiException extends Exception {
protected $headers; ///< Additional HTTP headers.
protected $user_data; ///< Data to output in HTTP body.
/**
* Create an API exception.
* @param string $message Message to be shown to the user.
* @param int $response_code HTTP response code to be set.
* @param string|array $headers additional headers to be set (e.g. WWW-Authenticate on 401).
*/
public function __construct($response_code = 502, $user_data = "", array $headers = array())
{
/* Informational and Success headers are of no use here.
* Obviously this would indicate a problem in our application logic.
*/
if ($response_code > 99 && $response_code < 300) $response_code = 500;
parent::__construct(CbHttpResponseCodes::get($response_code), $response_code);
$this->user_data = $user_data;
$this->headers = is_array($headers) ? $headers : array($headers);
}
/**
* Return the additional headers.
* @return array The headers.
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Output additional HTTP headers and response code header.
*/
public function outputHeaders()
{
foreach($this->getHeaders() as $header) header($header);
header("HTTP/1.0 ".$this->getCode()." ".$this->getMessage());
}
public function getUserData()
{
return $this->user_data;
}
}