1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 07:24:04 +00:00

Improve errors handling (#1381)

* replace `fmt.Errorf` to `errors.New`

* changed once `Sprintf` to `Join`
This commit is contained in:
Ilya Chukov 2021-06-13 12:11:42 +03:00 committed by GitHub
parent bca01cc9cc
commit 89fdbcf036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package msgp
import (
"fmt"
"reflect"
"strings"
)
const resumableDefault = false
@ -110,7 +111,7 @@ type errWrapped struct {
func (e errWrapped) Error() string {
if e.ctx != "" {
return fmt.Sprintf("%s at %s", e.cause, e.ctx)
return strings.Join([]string{e.cause.Error(), "at", e.ctx}, " ")
} else {
return e.cause.Error()
}

View File

@ -6,8 +6,9 @@ package uuid
import (
"encoding/binary"
"fmt"
"os"
"strconv"
"strings"
)
// A Domain represents a Version 2 domain
@ -76,5 +77,5 @@ func (d Domain) String() string {
case Org:
return "Org"
}
return fmt.Sprintf("Domain%d", int(d))
return strings.Join([]string{"Domain", strconv.Itoa(int(d))}, "")
}

View File

@ -4,7 +4,10 @@
package uuid
import "fmt"
import (
"errors"
"strconv"
)
// MarshalText implements encoding.TextMarshaler.
func (uuid UUID) MarshalText() ([]byte, error) {
@ -31,7 +34,7 @@ func (uuid UUID) MarshalBinary() ([]byte, error) {
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (uuid *UUID) UnmarshalBinary(data []byte) error {
if len(data) != 16 {
return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
return errors.New("invalid UUID (got " + strconv.Itoa(len(data)) + " bytes)")
}
copy(uuid[:], data)
return nil

View File

@ -6,6 +6,7 @@ package uuid
import (
"database/sql/driver"
"errors"
"fmt"
)
@ -26,7 +27,7 @@ func (uuid *UUID) Scan(src interface{}) error {
// see Parse for required string format
u, err := Parse(src)
if err != nil {
return fmt.Errorf("Scan: %v", err)
return errors.New("Scan: " + err.Error())
}
*uuid = u
@ -45,6 +46,7 @@ func (uuid *UUID) Scan(src interface{}) error {
copy((*uuid)[:], src)
default:
// here we use %T for type
return fmt.Errorf("Scan: unable to scan type %T into UUID", src)
}

View File

@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io"
"strconv"
"strings"
)
@ -49,7 +50,7 @@ func Parse(s string) (UUID, error) {
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
case 36 + 9:
if strings.ToLower(s[:9]) != "urn:uuid:" {
return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
return uuid, errors.New(`invalid urn prefix: "` + s[:9] + `"`)
}
s = s[9:]
@ -68,7 +69,7 @@ func Parse(s string) (UUID, error) {
}
return uuid, nil
default:
return uuid, fmt.Errorf("invalid UUID length: %d", len(s))
return uuid, errors.New("invalid UUID length: " + strconv.Itoa(len(s)))
}
// s is now at least 36 bytes long
// it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
@ -97,7 +98,7 @@ func ParseBytes(b []byte) (UUID, error) {
case 36: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
case 36 + 9: // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
return uuid, errors.New(`invalid urn prefix: "` + string(b[:9]) + `"`)
}
b = b[9:]
case 36 + 2: // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
@ -112,7 +113,7 @@ func ParseBytes(b []byte) (UUID, error) {
}
return uuid, nil
default:
return uuid, fmt.Errorf("invalid UUID length: %d", len(b))
return uuid, errors.New("invalid UUID length: " + strconv.Itoa(len(b)))
}
// s is now at least 36 bytes long
// it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx