Probe revamped to provide for a new WrappedError struct to wrap probes as error interface

This convenience was necessary to be used for golang library functions like io.Copy and io.Pipe
where we shouldn't be writing proxies and alternatives returning *probe.Error

This change also brings more changes across code base for clear separation regarding where an error
interface should be passed encapsulating *probe.Error and where it should be used as is.
This commit is contained in:
Harshavardhana
2015-08-07 23:47:22 -07:00
parent 28d9565400
commit 45b59b8456
34 changed files with 392 additions and 363 deletions

View File

@@ -24,7 +24,6 @@ import (
"os"
"time"
"github.com/minio/minio/pkg/probe"
"github.com/minio/minio/pkg/utils/log"
)
@@ -99,12 +98,12 @@ func fileLogger(filename string) (chan<- []byte, error) {
ch := make(chan []byte)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return nil, probe.New(err)
return nil, err
}
go func() {
for message := range ch {
if _, err := io.Copy(file, bytes.NewBuffer(message)); err != nil {
log.Errorln(probe.New(err))
log.Errorln(err)
}
}
}()

View File

@@ -41,7 +41,7 @@ func (r *httpRange) String() string {
}
// Grab new range from request header
func getRequestedRange(hrange string, size int64) (*httpRange, error) {
func getRequestedRange(hrange string, size int64) (*httpRange, *probe.Error) {
r := &httpRange{
start: 0,
length: 0,
@@ -51,16 +51,16 @@ func getRequestedRange(hrange string, size int64) (*httpRange, error) {
if hrange != "" {
err := r.parseRange(hrange)
if err != nil {
return nil, probe.New(err)
return nil, err.Trace()
}
}
return r, nil
}
func (r *httpRange) parse(ra string) error {
func (r *httpRange) parse(ra string) *probe.Error {
i := strings.Index(ra, "-")
if i < 0 {
return probe.New(donut.InvalidRange{})
return probe.NewError(donut.InvalidRange{})
}
start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
if start == "" {
@@ -68,7 +68,7 @@ func (r *httpRange) parse(ra string) error {
// range start relative to the end of the file.
i, err := strconv.ParseInt(end, 10, 64)
if err != nil {
return probe.New(donut.InvalidRange{})
return probe.NewError(donut.InvalidRange{})
}
if i > r.size {
i = r.size
@@ -78,7 +78,7 @@ func (r *httpRange) parse(ra string) error {
} else {
i, err := strconv.ParseInt(start, 10, 64)
if err != nil || i > r.size || i < 0 {
return probe.New(donut.InvalidRange{})
return probe.NewError(donut.InvalidRange{})
}
r.start = i
if end == "" {
@@ -87,7 +87,7 @@ func (r *httpRange) parse(ra string) error {
} else {
i, err := strconv.ParseInt(end, 10, 64)
if err != nil || r.start > i {
return probe.New(donut.InvalidRange{})
return probe.NewError(donut.InvalidRange{})
}
if i >= r.size {
i = r.size - 1
@@ -99,26 +99,26 @@ func (r *httpRange) parse(ra string) error {
}
// parseRange parses a Range header string as per RFC 2616.
func (r *httpRange) parseRange(s string) error {
func (r *httpRange) parseRange(s string) *probe.Error {
if s == "" {
return probe.New(errors.New("header not present"))
return probe.NewError(errors.New("header not present"))
}
if !strings.HasPrefix(s, b) {
return probe.New(donut.InvalidRange{})
return probe.NewError(donut.InvalidRange{})
}
ras := strings.Split(s[len(b):], ",")
if len(ras) == 0 {
return probe.New(errors.New("invalid request"))
return probe.NewError(errors.New("invalid request"))
}
// Just pick the first one and ignore the rest, we only support one range per object
if len(ras) > 1 {
return probe.New(errors.New("multiple ranges specified"))
return probe.NewError(errors.New("multiple ranges specified"))
}
ra := strings.TrimSpace(ras[0])
if ra == "" {
return probe.New(donut.InvalidRange{})
return probe.NewError(donut.InvalidRange{})
}
return r.parse(ra)
}

View File

@@ -72,7 +72,7 @@ func InitSignatureV4(req *http.Request) (*donut.Signature, *probe.Error) {
var err error
accessKeyID, err = StripAccessKeyID(ah)
if err != nil {
return nil, probe.New(err)
return nil, probe.NewError(err)
}
}
authConfig, err := auth.LoadConfig()
@@ -80,7 +80,7 @@ func InitSignatureV4(req *http.Request) (*donut.Signature, *probe.Error) {
return nil, err.Trace()
}
if _, ok := authConfig.Users[accessKeyID]; !ok {
return nil, probe.New(errors.New("AccessID not found"))
return nil, probe.NewError(errors.New("AccessID not found"))
}
signature := &donut.Signature{
AccessKeyID: authConfig.Users[accessKeyID].AccessKeyID,