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.
func writeDebugHeader(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"bytes"
"io"
"log"
"os"
"fmt"
"io/ioutil"
)
// bindata_read reads the given file from disk.
// It panics if anything went wrong.
func bindata_read(path, name string) []byte {
fd, err := os.Open(path)
// bindata_read reads the given file from disk. It returns
// an error on failure.
func bindata_read(path, name string) ([]byte, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Read %%s: %%v", name, err)
err = fmt.Errorf("Error reading asset %%s at %%s: %%v", name, path, 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()
return buf, err
}
`)
@ -66,7 +55,7 @@ func writeDebugAsset(w io.Writer, asset *Asset) error {
_, err := fmt.Fprintf(w, `
// %s reads file data from disk.
// It panics if something went wrong in the process.
func %s() []byte {
func %s() ([]byte, error) {
return bindata_read(
%q,
%q,

View File

@ -78,13 +78,13 @@ func header_compressed_nomemcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"bytes"
"compress/gzip"
"fmt"
"io"
"log"
"reflect"
"unsafe"
)
func bindata_read(data, name string) []byte {
func bindata_read(data, name string) ([]byte, error) {
var empty [0]byte
sx := (*reflect.StringHeader)(unsafe.Pointer(&data))
b := empty[:]
@ -95,7 +95,7 @@ func bindata_read(data, name string) []byte {
gz, err := gzip.NewReader(bytes.NewBuffer(b))
if err != nil {
log.Fatalf("Read %%q: %%v", name, err)
return nil, fmt.Errorf("Read %%q: %%v", name, err)
}
var buf bytes.Buffer
@ -103,10 +103,10 @@ func bindata_read(data, name string) []byte {
gz.Close()
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 (
"bytes"
"compress/gzip"
"fmt"
"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))
if err != nil {
log.Fatalf("Read %%q: %%v", name, err)
return nil, fmt.Errorf("Read %%q: %%v", name, err)
}
var buf bytes.Buffer
@ -132,10 +132,10 @@ func bindata_read(data []byte, name string) []byte {
gz.Close()
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 {
_, err := fmt.Fprintf(w, `import (
"fmt"
"reflect"
"unsafe"
)
func bindata_read(data, name string) []byte {
func bindata_read(data, name string) ([]byte, error) {
var empty [0]byte
sx := (*reflect.StringHeader)(unsafe.Pointer(&data))
b := empty[:]
@ -156,7 +157,7 @@ func bindata_read(data, name string) []byte {
bx.Data = sx.Data
bx.Len = len(data)
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 {
// nop -- We require no imports or helper functions.
return nil
_, err := fmt.Fprintf(w, `import (
"fmt"
)
`)
return err
}
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, `"
func %s() []byte {
func %s() ([]byte, error) {
return bindata_read(
_%s,
%q,
@ -196,7 +200,7 @@ func %s() []byte {
}
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)
if err != nil {
@ -234,7 +238,7 @@ func uncompressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err = fmt.Fprintf(w, `"
func %s() []byte {
func %s() ([]byte, error) {
return bindata_read(
_%s,
%q,
@ -246,7 +250,7 @@ func %s() []byte {
}
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)
if err != nil {
return err
@ -258,7 +262,7 @@ func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
}
_, 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 {
_, err := fmt.Fprintf(w, `
// Asset loads and returns the asset for the given name.
// This returns nil of the asset could not be found.
func Asset(name string) []byte {
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(name string) ([]byte, error) {
if f, ok := _bindata[name]; ok {
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.
var _bindata = map[string] func() []byte {
var _bindata = map[string] func() ([]byte, error) {
`)
return err
}