ObsException.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. namespace Obs;
  17. use GuzzleHttp\Psr7\Request;
  18. use GuzzleHttp\Psr7\Response;
  19. use Obs\Log\ObsLog;
  20. class ObsException extends \RuntimeException
  21. {
  22. const CLIENT = 'client';
  23. const SERVER = 'server';
  24. private $response;
  25. private $request;
  26. private $requestId;
  27. private $exceptionType;
  28. private $exceptionCode;
  29. private $exceptionMessage;
  30. private $hostId;
  31. public function __construct ($message = null, $code = null, $previous = null)
  32. {
  33. parent::__construct($message, $code, $previous);
  34. }
  35. public function setExceptionCode($exceptionCode)
  36. {
  37. $this->exceptionCode = $exceptionCode;
  38. }
  39. public function getExceptionCode()
  40. {
  41. return $this->exceptionCode;
  42. }
  43. public function setExceptionMessage($exceptionMessage)
  44. {
  45. $this->exceptionMessage = $exceptionMessage;
  46. }
  47. public function getExceptionMessage()
  48. {
  49. return $this->exceptionMessage ? $this->exceptionMessage : $this->message;
  50. }
  51. public function setExceptionType($exceptionType)
  52. {
  53. $this->exceptionType = $exceptionType;
  54. }
  55. public function getExceptionType()
  56. {
  57. return $this->exceptionType;
  58. }
  59. public function setRequestId($requestId)
  60. {
  61. $this->requestId = $requestId;
  62. }
  63. public function getRequestId()
  64. {
  65. return $this->requestId;
  66. }
  67. public function setResponse(Response $response)
  68. {
  69. $this->response = $response;
  70. }
  71. public function getResponse()
  72. {
  73. return $this->response;
  74. }
  75. public function setRequest(Request $request)
  76. {
  77. $this->request = $request;
  78. }
  79. public function getRequest()
  80. {
  81. return $this->request;
  82. }
  83. public function getStatusCode()
  84. {
  85. return $this->response ? $this->response->getStatusCode() : -1;
  86. }
  87. public function setHostId($hostId){
  88. $this->hostId = $hostId;
  89. }
  90. public function getHostId(){
  91. return $this->hostId;
  92. }
  93. public function __toString()
  94. {
  95. $message = get_class($this) . ': '
  96. . 'OBS Error Code: ' . $this->getExceptionCode() . ', '
  97. . 'Status Code: ' . $this->getStatusCode() . ', '
  98. . 'OBS Error Type: ' . $this->getExceptionType() . ', '
  99. . 'OBS Error Message: ' . ($this->getExceptionMessage() ? $this->getExceptionMessage():$this->getMessage());
  100. // Add the User-Agent if available
  101. if ($this->request) {
  102. $message .= ', ' . 'User-Agent: ' . $this->request->getHeaderLine('User-Agent');
  103. }
  104. $message .= "\n";
  105. ObsLog::commonLog(INFO, "http request:status:%d, %s",$this->getStatusCode(),"code:".$this->getExceptionCode().", message:".$this->getMessage());
  106. return $message;
  107. }
  108. }