1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // Errors in decoding and encoding are handled using panic and recover.
13 // Panics caused by user error (that is, everything except run-time panics
14 // such as "index out of bounds" errors) do not leave the file that caused
15 // them, but are instead turned into plain os.Error returns. Encoding and
16 // decoding functions and methods that do not return an os.Error either use
17 // panic to report an error or are guaranteed error-free.
19 // A gobError wraps an os.Error and is used to distinguish errors (panics) generated in this package.
20 type gobError
struct {
24 // errorf is like error but takes Printf-style arguments to construct an os.Error.
25 func errorf(format
string, args
...interface{}) {
26 error(fmt
.Errorf(format
, args
...))
29 // error wraps the argument error and uses it as the argument to panic.
30 func error(err os
.Error
) {
31 panic(gobError
{Error
: err
})
34 // catchError is meant to be used as a deferred function to turn a panic(gobError) into a
35 // plain os.Error. It overwrites the error return of the function that deferred its call.
36 func catchError(err
*os
.Error
) {
37 if e
:= recover(); e
!= nil {
38 *err
= e
.(gobError
).Error
// Will re-panic if not one of our errors, such as a runtime error.