Error handling in Upspin
This blog is worth a read even if you don&#x27;t care about Upspin or Go.  Some details are certainly specific to Upspin (and they&#x27;re marked as such), but there are also patterns worth thinking about for programmers of any background.<p>My favorite highlights:<p>&gt; it is critical that communications [between Upspin servers] preserve the structure of errors<p>YES.  This is something that&#x27;s wildly underconsidered in most projects and indeed most programming languages.  <i>Errors need to be serializable</i>... and <i>deserializable, losslessly</i>.  Having errors which can serialize round-trip is a superpower.  It unlocks the ability to have -- well, the blog gets to it later:<p>&gt; an <i>operational</i> trace, showing the path through the elements of the system, rather than as an <i>execution</i> trace, showing the path through the code. The distinction is vital.<p>YESSS. From personal anecdote: I&#x27;ve been writing a collection of software recently which is broken up into several different executables.  This is necessary and good in the system&#x27;s design, because they have different failure domains (it&#x27;s nice to send SIGINT to only one of them), and they also need to do some linux voodoo that&#x27;s process-level, yada yada.  The salient detail is that in this system, there are typically three processes deep in a tree for any user action.  That means I <i>need</i> to have errors from the third level down be reported <i>clearly</i>... <i>across process boundaries</i>.<p>An operational trace is the correct model for this kind of system.  Execution traces from any single process are relatively misguided.<p>&gt; The Kind field classifies the error as one of a set of standard conditions<p>This is something I&#x27;ve seen emerge from <i>many</i> programmers lately, independently (and from a variety of languages)!  There must be something good at the bottom of this idea if it&#x27;s so emergent.<p>The &quot;Kind&quot; idea is particularly useful and cross-cutting.  They&#x27;re serializable -- trivially, and non-recursively, because they&#x27;re a simple primitive type.  And, per the guidelines of what would be useful in a polyglot world, they&#x27;re also virtuous in that they&#x27;re pretty much an enum, which can be represented in any language, with or without typesystem-level help.<p>(I also have a library which builds on this concept; more about that later.)<p>---<p>Now, some things I disagree with:<p>Right after describing the importance of serializable errors, Pike goes on to mention:<p>&gt; we made Upspin&#x27;s RPCs aware of these error types<p>This is a colossal mistake.  Or rather, it&#x27;s a perfectly reasonable thing to do when developing a single project; but it severely limits the reusability of the technique, and also limits what we can learn from it.<p>I&#x27;m a polyglot.  I&#x27;d like to see a community -- with representatives from all languages -- focus on a simple definition of errors which can be reliably parsed and round-tripped to the wire and back in <i>any</i> language.  For gophers, a common example should be Go and Javascript: I would like my web front-end to understand my errors completely!<p>I think this is eminently doable.  Imagine a world where we could have the execution flow trace bounce between a whole series of microservices, losslessly!  It&#x27;s worth noting, however, that it would require an investment in keeping things simple; and in particular, controversially, <i>not</i> leaning on the language&#x27;s type system too much, since it will not translate well if your program would like to interact with programs in other languages.  Simplicity would be key here.  Maps, arrays, and strings.<p>---<p>Lastly, I said earlier I have a library.  It&#x27;s true.  (I have two libraries, actually; one in which I tried to use the Go type system more heavily, and included stack traces -- both of which I&#x27;ve come to regard as Mistakes over time and practice; I&#x27;ll not speak of that one.)<p>Gophers might be interested in this, but also I&#x27;d love commentary from folk of other language backgrounds:<p><a href="https:&#x2F;&#x2F;godoc.org&#x2F;github.com&#x2F;polydawn&#x2F;go-errcat" rel="nofollow">https:&#x2F;&#x2F;godoc.org&#x2F;github.com&#x2F;polydawn&#x2F;go-errcat</a><p>This library is an experiment, but using it in several projects has been very pleasing so far.  It has many of the same ideas as in this blog, particularly &quot;categories&quot; (which are a clear equivalent of &quot;kinds&quot; -- what&#x27;s in a name?).  Some ideas are taken slightly farther: namely, there is <i>one</i> schema for serializing these errors, and it very clearly coerces things into string types early in the program: the intention of this is to make sure your program logic has exactly as much information available to it as another program will if deserializing this error later; any more information being available in the original process would be a form of moral hazard, tempting you to write logic which would be impossible to replicate in external process.<p>The main experiment I have in mind with this library is <i>what&#x27;s the appropriate scope for a kind&#x2F;category?</i>  Does it map cleanly to packages?  Whole programs?  Something else entirely?