return errors from calls to Asset() instead of ever panicking

This commit is contained in:
Alan Shreve 2013-11-08 17:38:45 +02:00
parent 7ab45c816f
commit 8e87ecda2a
3 changed files with 37 additions and 43 deletions

View File

@ -30,29 +30,18 @@ func writeDebug(w io.Writer, toc []Asset) error {
// This targets debug builds. // This targets debug builds.
func writeDebugHeader(w io.Writer) error { func writeDebugHeader(w io.Writer) error {
_, err := fmt.Fprintf(w, `import ( _, err := fmt.Fprintf(w, `import (
"bytes" "fmt"
"io" "io/ioutil"
"log"
"os"
) )
// bindata_read reads the given file from disk. // bindata_read reads the given file from disk. It returns
// It panics if anything went wrong. // an error on failure.
func bindata_read(path, name string) []byte { func bindata_read(path, name string) ([]byte, error) {
fd, err := os.Open(path) buf, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
log.Fatalf("Read %%s: %%v", name, err) err = fmt.Errorf("Error reading asset %%s at %%s: %%v", name, path, err)
} }
return buf, err
defer fd.Close()
var buf bytes.Buffer
_, err = io.Copy(&buf, fd)
if err != nil {
log.Fatalf("Read %%s: %%v", name, err)
}
return buf.Bytes()
} }
`) `)
@ -66,7 +55,7 @@ func writeDebugAsset(w io.Writer, asset *Asset) error {
_, err := fmt.Fprintf(w, ` _, err := fmt.Fprintf(w, `
// %s reads file data from disk. // %s reads file data from disk.
// It panics if something went wrong in the process. // It panics if something went wrong in the process.
func %s() []byte { func %s() ([]byte, error) {
return bindata_read( return bindata_read(
%q, %q,
%q, %q,

View File

@ -78,13 +78,13 @@ func header_compressed_nomemcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import ( _, err := fmt.Fprintf(w, `import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"fmt"
"io" "io"
"log"
"reflect" "reflect"
"unsafe" "unsafe"
) )
func bindata_read(data, name string) []byte { func bindata_read(data, name string) ([]byte, error) {
var empty [0]byte var empty [0]byte
sx := (*reflect.StringHeader)(unsafe.Pointer(&data)) sx := (*reflect.StringHeader)(unsafe.Pointer(&data))
b := empty[:] b := empty[:]
@ -95,7 +95,7 @@ func bindata_read(data, name string) []byte {
gz, err := gzip.NewReader(bytes.NewBuffer(b)) gz, err := gzip.NewReader(bytes.NewBuffer(b))
if err != nil { if err != nil {
log.Fatalf("Read %%q: %%v", name, err) return nil, fmt.Errorf("Read %%q: %%v", name, err)
} }
var buf bytes.Buffer var buf bytes.Buffer
@ -103,10 +103,10 @@ func bindata_read(data, name string) []byte {
gz.Close() gz.Close()
if err != nil { if err != nil {
log.Fatalf("Read %%q: %%v", name, err) return nil, fmt.Errorf("Read %%q: %%v", name, err)
} }
return buf.Bytes() return buf.Bytes(), nil
} }
`) `)
@ -117,14 +117,14 @@ func header_compressed_memcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import ( _, err := fmt.Fprintf(w, `import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"fmt"
"io" "io"
"log"
) )
func bindata_read(data []byte, name string) []byte { func bindata_read(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data)) gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil { if err != nil {
log.Fatalf("Read %%q: %%v", name, err) return nil, fmt.Errorf("Read %%q: %%v", name, err)
} }
var buf bytes.Buffer var buf bytes.Buffer
@ -132,10 +132,10 @@ func bindata_read(data []byte, name string) []byte {
gz.Close() gz.Close()
if err != nil { if err != nil {
log.Fatalf("Read %%q: %%v", name, err) return nil, fmt.Errorf("Read %%q: %%v", name, err)
} }
return buf.Bytes() return buf.Bytes(), nil
} }
`) `)
@ -144,11 +144,12 @@ func bindata_read(data []byte, name string) []byte {
func header_uncompressed_nomemcopy(w io.Writer) error { func header_uncompressed_nomemcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import ( _, err := fmt.Fprintf(w, `import (
"fmt"
"reflect" "reflect"
"unsafe" "unsafe"
) )
func bindata_read(data, name string) []byte { func bindata_read(data, name string) ([]byte, error) {
var empty [0]byte var empty [0]byte
sx := (*reflect.StringHeader)(unsafe.Pointer(&data)) sx := (*reflect.StringHeader)(unsafe.Pointer(&data))
b := empty[:] b := empty[:]
@ -156,7 +157,7 @@ func bindata_read(data, name string) []byte {
bx.Data = sx.Data bx.Data = sx.Data
bx.Len = len(data) bx.Len = len(data)
bx.Cap = bx.Len bx.Cap = bx.Len
return b return b, nil
} }
`) `)
@ -164,8 +165,11 @@ func bindata_read(data, name string) []byte {
} }
func header_uncompressed_memcopy(w io.Writer) error { func header_uncompressed_memcopy(w io.Writer) error {
// nop -- We require no imports or helper functions. _, err := fmt.Fprintf(w, `import (
return nil "fmt"
)
`)
return err
} }
func compressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error { func compressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
@ -184,7 +188,7 @@ func compressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err = fmt.Fprintf(w, `" _, err = fmt.Fprintf(w, `"
func %s() []byte { func %s() ([]byte, error) {
return bindata_read( return bindata_read(
_%s, _%s,
%q, %q,
@ -196,7 +200,7 @@ func %s() []byte {
} }
func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error { func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err := fmt.Fprintf(w, `func %s() []byte { _, err := fmt.Fprintf(w, `func %s() ([]byte, error) {
return bindata_read([]byte{`, asset.Func) return bindata_read([]byte{`, asset.Func)
if err != nil { if err != nil {
@ -234,7 +238,7 @@ func uncompressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err = fmt.Fprintf(w, `" _, err = fmt.Fprintf(w, `"
func %s() []byte { func %s() ([]byte, error) {
return bindata_read( return bindata_read(
_%s, _%s,
%q, %q,
@ -246,7 +250,7 @@ func %s() []byte {
} }
func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error { func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err := fmt.Fprintf(w, `func %s() []byte { _, err := fmt.Fprintf(w, `func %s() ([]byte, error) {
return []byte{`, asset.Func) return []byte{`, asset.Func)
if err != nil { if err != nil {
return err return err
@ -258,7 +262,7 @@ func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
} }
_, err = fmt.Fprintf(w, ` _, err = fmt.Fprintf(w, `
} }, nil
} }
`) `)

9
toc.go
View File

@ -30,16 +30,17 @@ func writeTOC(w io.Writer, toc []Asset) error {
func writeTOCHeader(w io.Writer) error { func writeTOCHeader(w io.Writer) error {
_, err := fmt.Fprintf(w, ` _, err := fmt.Fprintf(w, `
// Asset loads and returns the asset for the given name. // Asset loads and returns the asset for the given name.
// This returns nil of the asset could not be found. // It returns an error if the asset could not be found or
func Asset(name string) []byte { // could not be loaded.
func Asset(name string) ([]byte, error) {
if f, ok := _bindata[name]; ok { if f, ok := _bindata[name]; ok {
return f() return f()
} }
return nil return nil, fmt.Errorf("Asset %%s not found", name)
} }
// _bindata is a table, holding each asset generator, mapped to its name. // _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string] func() []byte { var _bindata = map[string] func() ([]byte, error) {
`) `)
return err return err
} }