Exceptions.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. class HttpException extends \Exception{}
  3. /**
  4. * Returns an URL that automatically populates a new issue on GitHub based
  5. * on the information provided
  6. *
  7. * @param $title string Sets the title of the issue
  8. * @param $body string Sets the body of the issue (GitHub markdown applies)
  9. * @param $labels mixed (optional) Specifies labels to add to the issue
  10. * @param $maintainer string (optional) Specifies the maintainer for the issue.
  11. * The maintainer only applies if part of the development team!
  12. * @return string Returns a qualified URL to a new issue with populated conent.
  13. * Returns null if title or body is null or empty
  14. */
  15. function buildGitHubIssueQuery($title, $body, $labels = null, $maintainer = null){
  16. if(!isset($title) || !isset($body) || empty($title) || empty($body)) {
  17. return null;
  18. }
  19. // Add title and body
  20. $uri = 'https://github.com/rss-bridge/rss-bridge/issues/new?title='
  21. . urlencode($title)
  22. . '&body='
  23. . urlencode($body);
  24. // Add labels
  25. if(!is_null($labels) && is_array($labels) && count($labels) > 0) {
  26. if(count($lables) === 1) {
  27. $uri .= '&labels=' . urlencode($labels[0]);
  28. } else {
  29. foreach($labels as $label) {
  30. $uri .= '&labels[]=' . urlencode($label);
  31. }
  32. }
  33. } elseif(!is_null($labels) && is_string($labels)) {
  34. $uri .= '&labels=' . urlencode($labels);
  35. }
  36. // Add maintainer
  37. if(!empty($maintainer)) {
  38. $uri .= '&assignee=' . urlencode($maintainer);
  39. }
  40. return $uri;
  41. }
  42. /**
  43. * Returns the exception message as HTML string
  44. *
  45. * @param $e Exception The exception to show
  46. * @param $bridge object The bridge object
  47. * @return string Returns the exception as HTML string. Returns null if the
  48. * provided parameter are invalid
  49. */
  50. function buildBridgeException($e, $bridge){
  51. if(( !($e instanceof \Exception) && !($e instanceof \Error)) || !($bridge instanceof \BridgeInterface)) {
  52. return null;
  53. }
  54. $title = $bridge->getName() . ' failed with error ' . $e->getCode();
  55. // Build a GitHub compatible message
  56. $body = 'Error message: `'
  57. . $e->getMessage()
  58. . "`\nQuery string: `"
  59. . $_SERVER['QUERY_STRING']
  60. . "`\nVersion: `"
  61. . Configuration::getVersion()
  62. . '`';
  63. $link = buildGitHubIssueQuery($title, $body, 'bug report', $bridge->getMaintainer());
  64. $header = buildHeader($e, $bridge);
  65. $message = "<strong>{$bridge->getName()}</strong> was
  66. unable to receive or process the remote website's content!";
  67. $section = buildSection($e, $bridge, $message, $link);
  68. return buildPage($title, $header, $section);
  69. }
  70. /**
  71. * Returns the exception message as HTML string
  72. *
  73. * @param $e Exception The exception to show
  74. * @param $bridge object The bridge object
  75. * @return string Returns the exception as HTML string. Returns null if the
  76. * provided parameter are invalid
  77. */
  78. function buildTransformException($e, $bridge){
  79. if(( !($e instanceof \Exception) && !($e instanceof \Error)) || !($bridge instanceof \BridgeInterface)) {
  80. return null;
  81. }
  82. $title = $bridge->getName() . ' failed with error ' . $e->getCode();
  83. // Build a GitHub compatible message
  84. $body = 'Error message: `'
  85. . $e->getMessage()
  86. . "`\nQuery string: `"
  87. . $_SERVER['QUERY_STRING'] . '`';
  88. $link = buildGitHubIssueQuery($title, $body, 'bug report', $bridge->getMaintainer());
  89. $header = buildHeader($e, $bridge);
  90. $message = "RSS-Bridge was unable to transform the contents returned by
  91. <strong>{$bridge->getName()}</strong>!";
  92. $section = buildSection($e, $bridge, $message, $link);
  93. return buildPage($title, $header, $section);
  94. }
  95. function buildHeader($e, $bridge){
  96. return <<<EOD
  97. <header>
  98. <h1>Error {$e->getCode()}</h1>
  99. <h2>{$e->getMessage()}</h2>
  100. <p class="status">{$bridge->getName()}</p>
  101. </header>
  102. EOD;
  103. }
  104. function buildSection($e, $bridge, $message, $link){
  105. return <<<EOD
  106. <section>
  107. <p class="exception-message">{$message}</p>
  108. <div class="advice">
  109. <ul class="advice">
  110. <li>Press Return to check your input parameters</li>
  111. <li>Press F5 to retry</li>
  112. <li>Open a GitHub Issue if this error persists</li>
  113. </ul>
  114. </div>
  115. <a href="{$link}" title="After clicking this button you can review
  116. the issue before submitting it"><button>Open GitHub Issue</button></a>
  117. <p class="maintainer">{$bridge->getMaintainer()}</p>
  118. </section>
  119. EOD;
  120. }
  121. function buildPage($title, $header, $section){
  122. return <<<EOD
  123. <!DOCTYPE html>
  124. <html lang="en">
  125. <head>
  126. <title>{$title}</title>
  127. <link href="static/style.css" rel="stylesheet">
  128. </head>
  129. <body>
  130. {$header}
  131. {$section}
  132. </body>
  133. </html>
  134. EOD;
  135. }