diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-08 14:16:27 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-08 14:16:27 +0700 |
| commit | 0fde383e8f30a0d7504e7987f6a3ebe6a082d69a (patch) | |
| tree | f2e01fd4ebe26db08899a6ffc969ad4cbbf3970a /lib | |
| parent | e91954b5a65847970903e0d8294e54d918c8bc0b (diff) | |
| download | pakakeh.go-0fde383e8f30a0d7504e7987f6a3ebe6a082d69a.tar.xz | |
lib/ini: improve error message when parsing variable name
Display the invalid character in the error message with quote, so space
can detected.
Also, export the error variable so external caller can detect it using the
variable.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ini/reader.go | 22 | ||||
| -rw-r--r-- | lib/ini/reader_test.go | 55 |
2 files changed, 39 insertions, 38 deletions
diff --git a/lib/ini/reader.go b/lib/ini/reader.go index 39ed6d6e..642aeee2 100644 --- a/lib/ini/reader.go +++ b/lib/ini/reader.go @@ -1,6 +1,5 @@ -// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> -// // SPDX-License-Identifier: BSD-3-Clause +// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> package ini @@ -30,11 +29,14 @@ const ( tokUnderscore = '_' ) +// ErrVarNameChar define an error when variable name is using invalid +// character. +var ErrVarNameChar = errors.New(`invalid character in variable name`) + var ( - errBadConfig = errors.New("bad config line %d at %s") - errVarNoSection = "variable without section, line %d at %s" - errVarNameInvalid = errors.New("invalid variable name, line %d at %s") - errValueInvalid = errors.New("invalid value, line %d at %s") + errBadConfig = errors.New("bad config line %d at %s") + errVarNoSection = "variable without section, line %d at %s" + errValueInvalid = errors.New("invalid value, line %d at %s") ) // reader define the INI file reader. @@ -333,11 +335,11 @@ func (reader *reader) parseVariable() (err error) { reader.r, _, err = reader.br.ReadRune() if err != nil { - return errVarNameInvalid + return fmt.Errorf(`error when parsing variable name: %w`, err) } if !unicode.IsLetter(reader.r) { - return errVarNameInvalid + return fmt.Errorf(`%w %q`, ErrVarNameChar, reader.r) } var isNewline bool @@ -384,7 +386,7 @@ func (reader *reader) parseVariable() (err error) { return reader.parsePossibleValue() default: - return errVarNameInvalid + return fmt.Errorf(`%w %q`, ErrVarNameChar, reader.r) } } @@ -421,7 +423,7 @@ func (reader *reader) parsePossibleValue() (err error) { reader.bufFormat.WriteByte(reader.b) return reader.parseVarValue() default: - return errVarNameInvalid + return fmt.Errorf(`%w %q`, ErrVarNameChar, reader.b) } } diff --git a/lib/ini/reader_test.go b/lib/ini/reader_test.go index 18ef96b9..a1e0b256 100644 --- a/lib/ini/reader_test.go +++ b/lib/ini/reader_test.go @@ -1,6 +1,5 @@ -// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-License-Identifier: BSD-3-Clause +// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> package ini @@ -170,7 +169,7 @@ func TestParseVariable(t *testing.T) { cases := []struct { desc string in string - expErr error + expErr string expFormat string expKey string expValue string @@ -178,128 +177,128 @@ func TestParseVariable(t *testing.T) { expMode lineMode }{{ desc: `Empty`, - expErr: errVarNameInvalid, + expErr: `error when parsing variable name: EOF`, }, { desc: `Empty with space`, in: ` `, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name ' '`, }, { desc: `Digit at start`, in: `0name`, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name '0'`, }, { desc: `Digit at end`, in: `name0`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expFormat: `%s`, expKey: `name0`, }, { desc: `Digit at middle`, in: `na0me`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expFormat: `%s`, expKey: `na0me`, }, { desc: `Hyphen at start`, in: `-name`, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name '-'`, }, { desc: `Hyphen at end`, in: `name-`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expFormat: `%s`, expKey: `name-`, }, { desc: `Gyphen at middle`, in: `na-me`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expFormat: `%s`, expKey: `na-me`, }, { - desc: `Invalid chart at start`, + desc: `Invalid char at start`, in: `!name`, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name '!'`, }, { desc: `Invalid chart at end`, in: `name!`, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name '!'`, }, { desc: `Invalid char at middle`, in: `na!me`, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name '!'`, }, { desc: `With escaped char \\`, in: `na\me`, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name '\\'`, }, { desc: `Without space before comment`, in: `name; comment`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expKey: `name`, expFormat: `%s; comment`, }, { desc: `With space before comment`, in: `name ; comment`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expKey: `name`, expFormat: `%s ; comment`, }, { desc: `With empty value #1`, in: `name=`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyValue, expKey: `name`, expFormat: `%s=%s`, }, { desc: `With empty value #2`, in: `name =`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyValue, expKey: `name`, expFormat: `%s =%s`, }, { desc: `With empty value and comment`, in: `name = # a comment`, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyValue, expKey: `name`, expFormat: `%s =%s# a comment`, }, { desc: `With empty value #3`, in: `name `, - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expKey: `name`, expFormat: `%s `, }, { desc: `With newline`, in: "name \n", - expErr: io.EOF, + expErr: io.EOF.Error(), expMode: lineModeKeyOnly, expKey: `name`, expFormat: "%s \n", }, { desc: `With space in the middle`, in: `name 1`, - expErr: errVarNameInvalid, + expErr: `invalid character in variable name '1'`, }, { desc: `With dot`, in: `name.subname`, expMode: lineModeKeyOnly, - expErr: io.EOF, + expErr: io.EOF.Error(), expKey: `name.subname`, expFormat: `%s`, }, { desc: `With underscore char`, in: `name_subname`, expMode: lineModeKeyOnly, - expErr: io.EOF, + expErr: io.EOF.Error(), expKey: `name_subname`, expFormat: `%s`, }} @@ -313,7 +312,7 @@ func TestParseVariable(t *testing.T) { err := reader.parseVariable() if err != nil { - test.Assert(t, "error", c.expErr, err) + test.Assert(t, c.desc, c.expErr, err.Error()) if !errors.Is(err, io.EOF) { continue } |
