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.
9 // Errors in decoding and encoding are handled using panic and recover.
10 // Panics caused by user error (that is, everything except run-time panics
11 // such as "index out of bounds" errors) do not leave the file that caused
12 // them, but are instead turned into plain error returns. Encoding and
13 // decoding functions and methods that do not return an error either use
14 // panic to report an error or are guaranteed error-free.
16 // A gobError is used to distinguish errors (panics) generated in this package.
17 type gobError
struct {
21 // errorf is like error_ but takes Printf-style arguments to construct an error.
22 // It always prefixes the message with "gob: ".
23 func errorf(format
string, args
...any
) {
24 error_(fmt
.Errorf("gob: "+format
, args
...))
27 // error wraps the argument error and uses it as the argument to panic.
28 func error_(err error
) {
32 // catchError is meant to be used as a deferred function to turn a panic(gobError) into a
33 // plain error. It overwrites the error return of the function that deferred its call.
34 func catchError(err
*error
) {
35 if e
:= recover(); e
!= nil {
36 ge
, ok
:= e
.(gobError
)