Home | History | Annotate | Download | only in gob
      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.
      4 
      5 package gob
      6 
      7 import "fmt"
      8 
      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.
     15 
     16 // A gobError is used to distinguish errors (panics) generated in this package.
     17 type gobError struct {
     18 	err error
     19 }
     20 
     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 ...interface{}) {
     24 	error_(fmt.Errorf("gob: "+format, args...))
     25 }
     26 
     27 // error wraps the argument error and uses it as the argument to panic.
     28 func error_(err error) {
     29 	panic(gobError{err})
     30 }
     31 
     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)
     37 		if !ok {
     38 			panic(e)
     39 		}
     40 		*err = ge.err
     41 	}
     42 }
     43