Skip to content

Commit

Permalink
Change error string to Go standard
Browse files Browse the repository at this point in the history
  • Loading branch information
mhcerri committed Mar 7, 2016
1 parent 07314b0 commit 46052fe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion asn1.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (ctx *Context) setDefaultValue(value reflect.Value, opts *fieldOptions) err
value.SetUint(uint64(*opts.defaultValue))

default:
return syntaxError(ctx, "Default value is only allow to integers")
return syntaxError(ctx, "default value is only allowed to integers")
}
return nil
}
Expand Down
11 changes: 6 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewContext() *Context {
func (ctx *Context) getChoices(choice string) ([]choiceEntry, error) {
entries := ctx.choices[choice]
if entries == nil {
return nil, syntaxError(ctx, "Invalid choice \"%s\"", choice)
return nil, syntaxError(ctx, "invalid choice '%s'", choice)
}
return entries, nil
}
Expand All @@ -73,7 +73,7 @@ func (ctx *Context) getChoiceByType(choice string, t reflect.Type) (entry choice
return
}
}
err = syntaxError(ctx, "Invalid Go type \"%s\" for choice \"%s\"", t, choice)
err = syntaxError(ctx, "invalid Go type '%s' for choice '%s'", t, choice)
return
}

Expand All @@ -90,7 +90,8 @@ func (ctx *Context) getChoiceByTag(choice string, class, tag uint) (entry choice
return
}
}
err = syntaxError(ctx, "Invalid tag [%d,%d] for choice \"%s\"", class, tag, choice) // TODO
// TODO convert tag to text
err = syntaxError(ctx, "invalid tag [%d,%d] for choice '%s'", class, tag, choice)
return
}

Expand All @@ -99,7 +100,7 @@ func (ctx *Context) addChoiceEntry(choice string, entry choiceEntry) error {
for _, current := range ctx.choices[choice] {
if current.class == entry.class && current.tag == entry.tag {
return fmt.Errorf(
"Choice already registered: %s{%d, %d}",
"choice already registered: %s{%d, %d}",
choice, entry.class, entry.tag)
}
}
Expand Down Expand Up @@ -171,7 +172,7 @@ func (ctx *Context) AddChoice(choice string, entries []Choice) error {
if opts.choice != nil {
// TODO Add support for nested choices.
return syntaxError(ctx,
"nested choices are not allowed: \"%s\" inside \"%s\".",
"nested choices are not allowed: '%s' inside '%s'",
*opts.choice, choice)
}
raw := rawValue{}
Expand Down
20 changes: 10 additions & 10 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (ctx *Context) DecodeWithOptions(data []byte, obj interface{}, options stri
}

if !value.CanSet() {
return nil, syntaxError(ctx, "Go type \"%s\" is read-only", value.Type())
return nil, syntaxError(ctx, "go type '%s' is read-only", value.Type())
}

reader := bytes.NewBuffer(data)
Expand All @@ -167,7 +167,7 @@ func (ctx *Context) decode(reader io.Reader, value reflect.Value, opts *fieldOpt
return err
}
if ctx.der.decoding && raw.Indefinite {
return parseError(ctx, "Indefinite length form is not supported by DER mode")
return parseError(ctx, "indefinite length form is not supported by DER mode")
}

elem, err := ctx.getExpectedElement(raw, value.Type(), opts)
Expand All @@ -178,7 +178,7 @@ func (ctx *Context) decode(reader io.Reader, value reflect.Value, opts *fieldOpt
// And tag must match
if raw.Class != elem.class || raw.Tag != elem.tag {
ctx.log.Printf("%#v\n", opts)
return parseError(ctx, "Expected tag (%d,%d) but found (%d,%d)",
return parseError(ctx, "expected tag (%d,%d) but found (%d,%d)",
elem.class, elem.tag, raw.Class, raw.Tag)
}

Expand Down Expand Up @@ -245,7 +245,7 @@ func (ctx *Context) getExpectedElement(raw *rawValue, elemType reflect.Type, opt

// At this point a decoder function already be found
if elem.decoder == nil {
err = parseError(ctx, "Go type not supported \"%s\"", elemType)
err = parseError(ctx, "go type not supported '%s'", elemType)
}
return
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func (ctx *Context) getUniversalTag(objType reflect.Type, opts *fieldOptions) (e
if opts.set {
if elem.tag != tagSequence {
err = syntaxError(ctx,
"Flag \"set\" can't be set to Go type \"%s\"", objType)
"'set' cannot be used with Go type '%s'", objType)
}
elem.tag = tagSet
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func (ctx *Context) getRawValuesFromBytes(data []byte, max int) ([]*rawValue, er
return rawValues, nil
}
}
return nil, parseError(ctx, "Too many items for Sequence.")
return nil, parseError(ctx, "too many items for Sequence")
}

// matchExpectedValues tries to decode a sequence of raw values based on the
Expand Down Expand Up @@ -446,7 +446,7 @@ func (ctx *Context) setMissingFieldValue(e expectedFieldElement) error {
}
return nil
}
return parseError(ctx, "Missing value for [%d %d]", e.class, e.tag)
return parseError(ctx, "missing value for [%d %d]", e.class, e.tag)
}

// decodeStruct decodes struct fields in order
Expand Down Expand Up @@ -485,7 +485,7 @@ func (ctx *Context) decodeStructAsSet(data []byte, value reflect.Value) error {
prev := expectedElements[i-1]
if curr.class == prev.class &&
curr.tag == prev.tag {
return syntaxError(ctx, "Duplicated tag (%d,%d)", curr.class, curr.tag)
return syntaxError(ctx, "duplicated tag (%d,%d)", curr.class, curr.tag)
}
}

Expand Down Expand Up @@ -522,7 +522,7 @@ func (ctx *Context) decodeArray(data []byte, value reflect.Value) error {
var err error
for i := 0; i < value.Len(); i++ {
if len(data) == 0 {
return parseError(ctx, "Missing elements.")
return parseError(ctx, "missing elements")
}
elem := reflect.New(value.Type().Elem()).Elem()
data, err = ctx.DecodeWithOptions(data, elem.Addr().Interface(), "")
Expand All @@ -532,7 +532,7 @@ func (ctx *Context) decodeArray(data []byte, value reflect.Value) error {
value.Index(i).Set(elem)
}
if len(data) > 0 {
return parseError(ctx, "Too many elements.")
return parseError(ctx, "too many elements")
}
return nil
}
8 changes: 4 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (ctx *Context) encodeValue(value reflect.Value, opts *fieldOptions) (raw *r
}

if encoder == nil {
return nil, syntaxError(ctx, "Invalid Go type: %s", value.Type())
return nil, syntaxError(ctx, "invalid Go type: %s", value.Type())
}
raw.Content, err = encoder(value)
return
Expand All @@ -146,7 +146,7 @@ func (ctx *Context) applyOptions(value reflect.Value, raw *rawValue, opts *field
// Change sequence to set
if opts.set {
if raw.Class != classUniversal || raw.Tag != tagSequence {
return nil, syntaxError(ctx, "Go type \"%s\" does not accept the flag \"set\"", value.Type())
return nil, syntaxError(ctx, "Go type '%s' does not accept the flag 'set'", value.Type())
}
raw.Tag = tagSet
}
Expand All @@ -166,7 +166,7 @@ func (ctx *Context) applyOptions(value reflect.Value, raw *rawValue, opts *field
if opts.explicit {
if opts.tag == nil {
return nil, syntaxError(ctx,
"Invalid flag \"explicit\" without tag on Go type \"%s\".",
"invalid flag 'explicit' without tag on Go type '%s'",
value.Type())
}
content, err := raw.encode()
Expand Down Expand Up @@ -195,7 +195,7 @@ func (ctx *Context) applyOptions(value reflect.Value, raw *rawValue, opts *field
if opts.indefinite {
if !raw.Constructed {
return nil, syntaxError(ctx,
"Invalid flag \"indefinite\" on Go type: %s",
"invalid flag 'indefinite' on Go type: %s",
value.Type())
}
raw.Indefinite = true
Expand Down
14 changes: 7 additions & 7 deletions raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (raw *rawValue) encode() ([]byte, error) {
} else {
// Indefinite length uses 0x80, data..., 0x00, 0x00
if !raw.Constructed {
return nil, fmt.Errorf("Indefinite length is only allowed to constructed types")
return nil, fmt.Errorf("indefinite length is only allowed to constructed types")
}
buf = append(buf, 0x80)
buf = append(buf, raw.Content...)
Expand All @@ -79,7 +79,7 @@ func (raw *rawValue) encode() ([]byte, error) {
func encodeIdentifier(node *rawValue) ([]byte, error) {

if node.Class > 0x03 {
return nil, fmt.Errorf("Invalid class value: %d", node.Class)
return nil, fmt.Errorf("invalid class value: %d", node.Class)
}

identifier := []byte{0x00}
Expand Down Expand Up @@ -172,7 +172,7 @@ func decodeRawValue(reader io.Reader) (*rawValue, error) {
return nil, err
}
if indefinite && !constructed {
return nil, fmt.Errorf("Primitive node with indefinite length.")
return nil, fmt.Errorf("primitive node with indefinite length")
}

// Indefinite form
Expand Down Expand Up @@ -213,7 +213,7 @@ func readEoc(reader io.Reader) error {
}

if indefinite && !constructed {
return fmt.Errorf("Primitive node with indefinite length.")
return fmt.Errorf("primitive node with indefinite length")
}

if class == 0 && tag == 0 && indefinite == false && length == 0 {
Expand Down Expand Up @@ -244,7 +244,7 @@ func decodeMultiByteTag(reader io.Reader) (uint, error) {
// if we need to shift out non zeros bits, so the tag is too big for an uint
msb := uint64(0xfe) << (intBits - 8) // 7 most significant bits
if uint64(tag)&msb != 0 {
return 0, fmt.Errorf("Multi byte tag too big")
return 0, fmt.Errorf("multi byte tag too big")
}
// Shift the previous value and add the new 7 bits
tag = (tag << 7) | uint(b&0x7f)
Expand Down Expand Up @@ -303,7 +303,7 @@ func decodeLength(reader io.Reader) (length uint, indefinite bool, err error) {

// Long form
if b == 0xff {
err = fmt.Errorf("Invalid number of length octets: %x", b)
err = fmt.Errorf("invalid number of length octets: %x", b)
return
}
octets := make([]byte, int(b&0x7f))
Expand All @@ -314,7 +314,7 @@ func decodeLength(reader io.Reader) (length uint, indefinite bool, err error) {
for _, b = range octets {
msb := uint64(0xff) << (intBits - 8)
if uint64(length)&msb != 0 {
err = fmt.Errorf("Multi byte length too big")
err = fmt.Errorf("multi byte length too big")
return
}
length = (length << 8) | uint(b)
Expand Down
22 changes: 11 additions & 11 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (ctx *Context) decodeBool(data []byte, value reflect.Value) error {
return nil
}
}
return parseError(ctx, "Invalid bool value.")
return parseError(ctx, "invalid bool value")
}

func (ctx *Context) encodeBigInt(value reflect.Value) ([]byte, error) {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (ctx *Context) decodeInt(data []byte, value reflect.Value) error {
return err
}
if len(data) > 8 {
return parseError(ctx, "Integer too large for Go type \"%s\"", value.Type())
return parseError(ctx, "integer too large for Go type '%s'", value.Type())
}
// Sign extend the value
extensionByte := byte(0x00)
Expand Down Expand Up @@ -162,10 +162,10 @@ func (ctx *Context) decodeUint(data []byte, value reflect.Value) error {
return err
}
if len(data) > 8 {
return parseError(ctx, "Integer too large for Go type \"%s\"", value.Type())
return parseError(ctx, "integer too large for Go type '%s'", value.Type())
}
if len(data) > 0 && data[0]&0x80 != 0 {
return parseError(ctx, "Negative integer can't be assigned to Go type \"%s\"", value.Type())
return parseError(ctx, "negative integer can't be assigned to Go type '%s'", value.Type())
}
num := uint64(0)
for i := 0; i < len(data); i++ {
Expand Down Expand Up @@ -277,15 +277,15 @@ func (ctx *Context) encodeOid(value reflect.Value) ([]byte, error) {
if len(oid) >= 1 {
value1 = oid[0]
if value1 > 2 {
return nil, parseError(ctx, "Invalid value for first element of OID: %d", value1)
return nil, parseError(ctx, "invalid value for first element of OID: %d", value1)
}
}

value2 := uint(0)
if len(oid) >= 2 {
value2 = oid[1]
if value2 > 39 {
return nil, parseError(ctx, "Invalid value for first element of OID: %d", value2)
return nil, parseError(ctx, "invalid value for first element of OID: %d", value2)
}
}

Expand All @@ -311,7 +311,7 @@ func (ctx *Context) decodeOid(data []byte, value reflect.Value) error {
for reader.Len() > 0 {
valueN, err := decodeMultiByteTag(reader)
if err != nil {
return fmt.Errorf("Invalid value element in Object Identifier.")
return fmt.Errorf("invalid value element in Object Identifier")
}
oid = append(oid, valueN)
}
Expand All @@ -335,10 +335,10 @@ func (ctx *Context) decodeNull(data []byte, value reflect.Value) error {
// TODO check value type
_, ok := value.Interface().(Null)
if !ok {
return syntaxError(ctx, "Invalid type: %s", value.Type())
return syntaxError(ctx, "invalid type: %s", value.Type())
}
if len(data) != 0 {
return parseError(ctx, "Invalid data for Null type")
return parseError(ctx, "invalid data for Null type")
}
return nil
}
Expand All @@ -352,7 +352,7 @@ func checkInt(ctx *Context, data []byte) error {
if len(data) >= 2 {
if data[0] == 0xff || data[0] == 0x00 {
if data[0]&0x80 == data[1]&0x80 {
return parseError(ctx, "Integer not encoded in the short form")
return parseError(ctx, "integer not encoded in the short form")
}
}
}
Expand Down Expand Up @@ -399,6 +399,6 @@ func removeIntLeadingBytes(buf []byte) []byte {

func wrongType(ctx *Context, typeName string, value reflect.Value) error {
return syntaxError(ctx,
"Invalid Go type \"%s\" found when expecting \"%s\"",
"invalid Go type '%s' found when expecting '%s'",
value.Type(), typeName)
}

0 comments on commit 46052fe

Please sign in to comment.