Exceptions.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $link = buildGitHubIssueQuery($title, $body, 'bug report', $bridge->getMaintainer());
  61. $header = buildHeader($e, $bridge);
  62. $message = "<strong>{$bridge->getName()}</strong> was
  63. unable to receive or process the remote website's content!";
  64. $section = buildSection($e, $bridge, $message, $link);
  65. return buildPage($title, $header, $section);
  66. }
  67. /**
  68. * Returns the exception message as HTML string
  69. *
  70. * @param $e Exception The exception to show
  71. * @param $bridge object The bridge object
  72. * @return string Returns the exception as HTML string. Returns null if the
  73. * provided parameter are invalid
  74. */
  75. function buildTransformException($e, $bridge){
  76. if(( !($e instanceof \Exception) && !($e instanceof \Error)) || !($bridge instanceof \BridgeInterface)) {
  77. return null;
  78. }
  79. $title = $bridge->getName() . ' failed with error ' . $e->getCode();
  80. // Build a GitHub compatible message
  81. $body = 'Error message: `'
  82. . $e->getMessage()
  83. . "`\nQuery string: `"
  84. . $_SERVER['QUERY_STRING'] . '`';
  85. $link = buildGitHubIssueQuery($title, $body, 'bug report', $bridge->getMaintainer());
  86. $header = buildHeader($e, $bridge);
  87. $message = "RSS-Bridge was unable to transform the contents returned by
  88. <strong>{$bridge->getName()}</strong>!";
  89. $section = buildSection($e, $bridge, $message, $link);
  90. return buildPage($title, $header, $section);
  91. }
  92. function buildHeader($e, $bridge){
  93. return <<<EOD
  94. <header>
  95. <h1>Error {$e->getCode()}</h1>
  96. <h2>{$e->getMessage()}</h2>
  97. <p class="status">{$bridge->getName()}</p>
  98. </header>
  99. EOD;
  100. }
  101. function buildSection($e, $bridge, $message, $link){
  102. return <<<EOD
  103. <section>
  104. <p class="exception-message">{$message}</p>
  105. <div class="advice">
  106. <ul class="advice">
  107. <li>Press Return to check your input parameters</li>
  108. <li>Press F5 to retry</li>
  109. <li>Open a GitHub Issue if this error persists</li>
  110. </ul>
  111. </div>
  112. <a href="{$link}" title="After clicking this button you can review
  113. the issue before submitting it"><button>Open GitHub Issue</button></a>
  114. <p class="maintainer">{$bridge->getMaintainer()}</p>
  115. </section>
  116. EOD;
  117. }
  118. function buildPage($title, $header, $section){
  119. return <<<EOD
  120. <!DOCTYPE html>
  121. <html lang="en">
  122. <head>
  123. <title>{$title}</title>
  124. <link href="static/style.css" rel="stylesheet">
  125. </head>
  126. <body>
  127. {$header}
  128. {$section}
  129. </body>
  130. </html>
  131. EOD;
  132. }