09.xhtml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. h t t :: / / t /
  4. h t t :: // // t //
  5. h ttttt ttttt ppppp sssss // // y y sssss ttttt //
  6. hhhh t t p p s // // y y s t //
  7. h hh t t ppppp sssss // // yyyyy sssss t //
  8. h h t t p s :: / / y .. s t .. /
  9. h h t t p sssss :: / / yyyyy .. sssss t .. /
  10. <https://y.st./>
  11. Copyright © 2016 Alex Yst <mailto:copyright@y.st>
  12. This program is free software: you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation, either version 3 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program. If not, see <https://www.gnu.org./licenses/>.
  22. -->
  23. <!DOCTYPE html>
  24. <html xmlns="http://www.w3.org/1999/xhtml">
  25. <head>
  26. <base href="https://y.st./en/weblog/2016/02-February/09.xhtml" />
  27. <title>Making better use of exceptions and an attempt at database freedom &lt;https://y.st./en/weblog/2016/02-February/09.xhtml&gt;</title>
  28. <link rel="icon" type="image/png" href="/link/CC_BY-SA_4.0/y.st./icon.png" />
  29. <link rel="stylesheet" type="text/css" href="/link/basic.css" />
  30. <link rel="stylesheet" type="text/css" href="/link/site-specific.css" />
  31. <script type="text/javascript" src="/script/javascript.js" />
  32. <meta name="viewport" content="width=device-width" />
  33. </head>
  34. <body>
  35. <nav>
  36. <p>
  37. <a href="/en/">Home</a> |
  38. <a href="/en/a/about.xhtml">About</a> |
  39. <a href="/en/a/contact.xhtml">Contact</a> |
  40. <a href="/a/canary.txt">Canary</a> |
  41. <a href="/en/URI_research/"><abbr title="Uniform Resource Identifier">URI</abbr> research</a> |
  42. <a href="/en/opinion/">Opinions</a> |
  43. <a href="/en/coursework/">Coursework</a> |
  44. <a href="/en/law/">Law</a> |
  45. <a href="/en/a/links.xhtml">Links</a> |
  46. <a href="/en/weblog/2016/02-February/09.xhtml.asc">{this page}.asc</a>
  47. </p>
  48. <hr/>
  49. <p>
  50. Weblog index:
  51. <a href="/en/weblog/"><abbr title="American Standard Code for Information Interchange">ASCII</abbr> calendars</a> |
  52. <a href="/en/weblog/index_ol_ascending.xhtml">Ascending list</a> |
  53. <a href="/en/weblog/index_ol_descending.xhtml">Descending list</a>
  54. </p>
  55. <hr/>
  56. <p>
  57. Jump to entry:
  58. <a href="/en/weblog/2015/03-March/07.xhtml">&lt;&lt;First</a>
  59. <a rel="prev" href="/en/weblog/2016/02-February/08.xhtml">&lt;Previous</a>
  60. <a rel="next" href="/en/weblog/2016/02-February/10.xhtml">Next&gt;</a>
  61. <a href="/en/weblog/latest.xhtml">Latest&gt;&gt;</a>
  62. </p>
  63. <hr/>
  64. </nav>
  65. <header>
  66. <h1>Making better use of exceptions and an attempt at database freedom</h1>
  67. <p>Day 00339: Tuesday, 2016 February 09</p>
  68. </header>
  69. <p>
  70. Today, I thought that it would be a good idea to clean up my use of exceptions.
  71. Until today, I have been throwing exceptions of predefined types without giving my exceptions any distinguishing features.
  72. Sure, I set the error messages of the exceptions, but those are not very useful when determining if you have caught the expected exception.
  73. There are a few ways that I could make my exceptions stand out.
  74. The easiest would be to define my own exception classes for each situation in which I need to throw an exception.
  75. This seems like overkill though, as least for my current use case.
  76. The second option is to set an error code and use the exception&apos;s <a href="https://secure.php.net/manual/en/exception.getcode.php"><code>getCode()</code></a> method to retrieve it and compare it against a known value.
  77. The problem with this option is that the default exceptions only accept integer error codes.
  78. If I could use a string, I could set the code to the name of the throwing class/function, so I could distinguish between exceptions thrown by the class/function that I am expecting an exception from and exceptions thrown because of unexpected errors.
  79. Again, I could define my own exception classes in order to add the option of using string error codes, but I would either be giving up the option of using a fitting exception class for each case or I would need to define several essentially-identical exceptions, each of which extend a different predefined exception class.
  80. My next option would be to choose a large integer that people are not likely to choose and use that as the exception code.
  81. This has the advantage that the standard exception classes could still be used, but the disadvantage that the integers are not easily memorable or easily recognizable.
  82. They could be easily tested against if thrown from within a class though, as the class could have a constant <code>self::EXCEPTION_CODE</code>, which would be visible and comparable within <code>catch()</code> blocks.
  83. This would not be helpful for exceptions thrown from functions, but would work for my use case.
  84. I throw exceptions from functions only when I want the calling script to die.
  85. In classes though, throwing exceptions is useful for preventing instantiation, in which case the script may still need to continue.
  86. My final option would be to set an unused property of the exception to the string value that I desire.
  87. That has the advantage of not needing any new exceptions defined and still having recognizable values, but relies on people using include.d to recognize the fact that although the exceptions I throw are of the standard type, but that they are somehow different and may need to be treated different.
  88. That just sounds like messy coding.
  89. </p>
  90. <p>
  91. I decided to define the <code>self::EXCEPTION_CODE</code> constant and use integer exception codes based on the hash of the class&apos; name.
  92. To fit within the bounds of the integer value type, it does need to be truncated and wrapped from unsigned signed.
  93. It seems that if a float is used during class instantiation, no error will be raised, but the float will be ignored and the default error code of zero will be used.
  94. To provide better cross-platform support, I will keep my error codes within the 32-bit integer range.
  95. Once I had the <code>self::EXCEPTION_CODE</code> constant implemented, I found an exception method that made a lot more sense to use: <a href="https://secure.php.net/manual/en/exception.getfile.php"><code>\Exception::getFile()</code></a>.
  96. This method returns the name of the file in which the exception was thrown, which is a much more logical way to determine if the error came from a particular one of my classes or functions, seeing as each class and function is in its own file.
  97. Classes can implement a constant that is set using the <code>__FILE__</code> keyword for ease, but determining the origin of an exception thrown from a function can be fairly-well identified by running the output of <code>\Exception::getFile()</code> through <code>\substr()</code>.
  98. That freed up the error code value to be used for more fine-tuned processing.
  99. I am not sure that I have a use for the error codes myself, but I have added a different error code for each line that throws an exception so that these cases can be handled differently if needed.
  100. </p>
  101. <p>
  102. After setting proper error codes, I thought about using that same hashing technique for setting the values of constants that are meant to be used as options.
  103. These values are arbitrary, and I had been setting them according to alphabetical order of the constants used by a given class for options for input into functions.
  104. The hashing idea seems like a good option though.
  105. After all, these constants are meant to be used as-is; their values are arbitrary and should not be used directly.
  106. However, I decided on something better.
  107. To express both the arbitrary nature of these values and the fact that they are volatile and subject to change, I can set them using the <code>__LINE__</code> keyword.
  108. It is useful to have the exception codes be unique across classes, but the option codes do not need to be.
  109. There is no point in hashing anything meaningful, I just need to take advantage of the arbitrary data that exists simply because that line of code exists.
  110. I checked through my classes though, and it appears that I no longer have any such option constants.
  111. I must have only used them with wrapper classes so far, so when I deleted most of those, I deleted the constants with them.
  112. I will use this approach for any relevant classes in the future.
  113. </p>
  114. <p>
  115. Having finished my improvements to my exception-based code, I next focused on my intended task for the day: converting the spider to use the <a href="https://secure.php.net/manual/en/class.pdo.php"><code>\PDO</code> class</a> instead of the <a href="https://secure.php.net/manual/en/class.mysqli.php"><code>\mysqli</code> class</a>.
  116. I had planned for a simple method swap for the most part, but the documentation for the <a href="https://secure.php.net/manual/en/pdo.quote.php"><code>\PDO::quote()</code> method</a> insisted that I should instead use <a href="https://secure.php.net/manual/en/pdo.prepare.php"><code>\PDO::prepare()</code></a> to generate prepared statements that would later be run by passing an array of values instead of a query in string form.
  117. Reluctantly, I restructured all of my database logic to accommodate.
  118. The <a href="https://secure.php.net/manual/en/pdostatement.execute.php"><code>\PDOStatement::execute()</code> method</a> is less than pleasant to use.
  119. It can be used in two ways, one of which seems very messy and requires needless extra legwork and the other of which just requires needless extra leg work.
  120. If I understand correctly, the first option requires you to set up variables that are not passed into the method, but act as both input into and output from the method, presumably in some sort of bizarre pass-by-reference move taken to the next level of strangeness.
  121. Also, the output retrieved using the more sensible option returns arrays with double the needed number of elements by default.
  122. One set of elements is indexed by name, the other by integer.
  123. I never could find a way to make the class return arrays using only the named indexes, but there was an option for making the class return instances of another class instead of arrays.
  124. These class instances had only named properties, no numbered properties.
  125. This option allow any arbitrary class be used for the purpose, or presumably, any arbitrary class that allows simple instantiation and allows public access to the properties that need to be set (which vary based on the query being performed).
  126. My main goal was just to get rid of the extra unneeded numeric keys, I did not need any special functionality.
  127. With that in mind, I just used the generic <code>\stdClass</code>, which worked perfectly.
  128. In any case, hopefully the new code will be compatible with several database options.
  129. </p>
  130. <p>
  131. While working with the spider code, I found a strange error in how sitemaps were being handled.
  132. Instead of adding them to the queue, the spider was setting the current-page <abbr title="Uniform Resource Identifier">URI</abbr> to that of the sitemap.
  133. Before this change could have any effect, the current-page <abbr title="Uniform Resource Identifier">URI</abbr> was set to that of a page from the queue, so sitemaps were essentially being ignored.
  134. I also found a section of the code that had not been updated to use the <code>\DOMDocument</code> class&apos; output, still relying on the output format of my <code>wrapper\xml</code> class.
  135. The spider was two days into crawling a website that it said it had four days left to work on, but I decided to kill it prematurely anyway.
  136. I will not be here when the spider finishes with that site and I do not want the data format error to kill the spider while I am away, nor do I want the spider to continue ignoring sitemaps.
  137. The sitemap fix is probably just a formality though.
  138. I am not sure that many people in onion space expect search engine spiders to come crawling, so they likely do not have <code>robots.txt</code> files or sitemaps set up.
  139. </p>
  140. <p>
  141. While learning how to use the <code>\PDO</code> class and its related classes, I found a new exception class: <code>\PDOException</code>.
  142. My understanding of the exception inheritance tree is now as follows:
  143. </p>
  144. <ul>
  145. <li><a href="https://secure.php.net/manual/en/class.throwable.php"><code>\Throwable</code></a><ul>
  146. <li><a href="https://secure.php.net/manual/en/class.error.php"><code>\Error</code></a><ul>
  147. <li><a href="https://secure.php.net/manual/en/class.arithmeticerror.php"><code>\ArithmeticError</code></a><ul>
  148. <li><a href="https://secure.php.net/manual/en/class.divisionbyzeroerror.php"><code>\DivisionByZeroError</code></a></li>
  149. </ul></li>
  150. <li><a href="https://secure.php.net/manual/en/class.assertionerror.php"><code>\AssertionError</code></a></li>
  151. <li><a href="https://secure.php.net/manual/en/class.parseerror.php"><code>\ParseError</code></a></li>
  152. <li><a href="https://secure.php.net/manual/en/class.typeerror.php"><code>\TypeError</code></a></li>
  153. </ul></li>
  154. <li><a href="https://secure.php.net/manual/en/class.exception.php"><code>\Exception</code></a><ul>
  155. <li><a href="https://secure.php.net/manual/en/class.errorexception.php"><code>\ErrorException</code></a></li>
  156. <li><a href="https://secure.php.net/manual/en/class.logicexception.php"><code>\LogicException</code></a><ul>
  157. <li><a href="https://secure.php.net/manual/en/class.badfunctioncallexception.php"><code>\BadFunctionCallException</code></a><ul>
  158. <li><a href="https://secure.php.net/manual/en/class.badmethodcallexception.php"><code>\BadMethodCallException</code></a></li>
  159. </ul></li>
  160. <li><a href="https://secure.php.net/manual/en/class.domainexception.php"><code>\DomainException</code></a></li>
  161. <li><a href="https://secure.php.net/manual/en/class.invalidargumentexception.php"><code>\InvalidArgumentException</code></a></li>
  162. <li><a href="https://secure.php.net/manual/en/class.lengthexception.php"><code>\LengthException</code></a></li>
  163. <li><a href="https://secure.php.net/manual/en/class.outofrangeexception.php"><code>\OutOfRangeException</code></a></li>
  164. </ul></li>
  165. <li><a href="https://secure.php.net/manual/en/class.runtimeexception.php"><code>\RuntimeException</code></a><ul>
  166. <li><a href="https://secure.php.net/manual/en/class.outofboundsexception.php"><code>\OutOfBoundsException</code></a></li>
  167. <li><a href="https://secure.php.net/manual/en/class.overflowexception.php"><code>\OverflowException</code></a></li>
  168. <li><a href="https://secure.php.net/manual/en/class.pdoexception.php"><code>\PDOException</code></a></li>
  169. <li><a href="https://secure.php.net/manual/en/class.rangeexception.php"><code>\RangeException</code></a></li>
  170. <li><a href="https://secure.php.net/manual/en/class.underflowexception.php"><code>\UnderflowException</code></a></li>
  171. <li><a href="https://secure.php.net/manual/en/class.unexpectedvalueexception.php"><code>\UnexpectedValueException</code></a></li>
  172. </ul></li>
  173. </ul></li>
  174. </ul></li>
  175. </ul>
  176. <hr/>
  177. <p>
  178. Copyright © 2016 Alex Yst;
  179. You may modify and/or redistribute this document under the terms of the <a rel="license" href="/license/gpl-3.0-standalone.xhtml"><abbr title="GNU&apos;s Not Unix">GNU</abbr> <abbr title="General Public License version Three or later">GPLv3+</abbr></a>.
  180. If for some reason you would prefer to modify and/or distribute this document under other free copyleft terms, please ask me via email.
  181. My address is in the source comments near the top of this document.
  182. This license also applies to embedded content such as images.
  183. For more information on that, see <a href="/en/a/licensing.xhtml">licensing</a>.
  184. </p>
  185. <p>
  186. <abbr title="World Wide Web Consortium">W3C</abbr> standards are important.
  187. This document conforms to the <a href="https://validator.w3.org./nu/?doc=https%3A%2F%2Fy.st.%2Fen%2Fweblog%2F2016%2F02-February%2F09.xhtml"><abbr title="Extensible Hypertext Markup Language">XHTML</abbr> 5.1</a> specification and uses style sheets that conform to the <a href="http://jigsaw.w3.org./css-validator/validator?uri=https%3A%2F%2Fy.st.%2Fen%2Fweblog%2F2016%2F02-February%2F09.xhtml"><abbr title="Cascading Style Sheets">CSS</abbr>3</a> specification.
  188. </p>
  189. </body>
  190. </html>