Skip to content

Commit

Permalink
get more information about error (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: li_kuankuan <li_kuankuan@topsec.com.cn>
Co-authored-by: Joe Chen <jc@unknwon.io>
  • Loading branch information
3 people committed Nov 11, 2021
1 parent 0a525b4 commit 3956362
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (c *ConfigFile) GetValue(section, key string) (string, error) {
// Check if section exists
if _, ok := c.data[section]; !ok {
// Section does not exist.
return "", getError{ERR_SECTION_NOT_FOUND, section}
return "", GetError{ERR_SECTION_NOT_FOUND, section}
}

// Section exists.
Expand All @@ -189,7 +189,7 @@ func (c *ConfigFile) GetValue(section, key string) (string, error) {
}

// Return empty value.
return "", getError{ERR_KEY_NOT_FOUND, key}
return "", GetError{ERR_KEY_NOT_FOUND, key}
}

// Key exists.
Expand Down Expand Up @@ -436,7 +436,7 @@ func (c *ConfigFile) GetSection(section string) (map[string]string, error) {
// Check if section exists.
if _, ok := c.data[section]; !ok {
// Section does not exist.
return nil, getError{ERR_SECTION_NOT_FOUND, section}
return nil, GetError{ERR_SECTION_NOT_FOUND, section}
}

// Remove pre-defined key.
Expand Down Expand Up @@ -544,14 +544,14 @@ func (c *ConfigFile) SetPrettyFormat(pretty bool) {
c.prettyFormat = pretty
}

// getError occurs when get value in configuration file with invalid parameter.
type getError struct {
// GetError occurs when get value in configuration file with invalid parameter.
type GetError struct {
Reason ParseError
Name string
}

// Error implements Error interface.
func (err getError) Error() string {
func (err GetError) Error() string {
switch err.Reason {
case ERR_SECTION_NOT_FOUND:
return fmt.Sprintf("section '%s' not found", err.Name)
Expand Down
16 changes: 8 additions & 8 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *ConfigFile) read(reader io.Reader) (err error) {
count = 1
continue
case section == "": // No section defined so far
return readError{ERR_BLANK_SECTION_NAME, line}
return ReadError{ERR_BLANK_SECTION_NAME, line}
default: // Other alternatives
var (
i int
Expand All @@ -109,19 +109,19 @@ func (c *ConfigFile) read(reader io.Reader) (err error) {
qLen := len(keyQuote)
pos := strings.Index(line[qLen:], keyQuote)
if pos == -1 {
return readError{ERR_COULD_NOT_PARSE, line}
return ReadError{ERR_COULD_NOT_PARSE, line}
}
pos = pos + qLen
i = strings.IndexAny(line[pos:], "=:")
if i <= 0 {
return readError{ERR_COULD_NOT_PARSE, line}
return ReadError{ERR_COULD_NOT_PARSE, line}
}
i = i + pos
key = line[qLen:pos] //保留引号内的两端的空格
} else {
i = strings.IndexAny(line, "=:")
if i <= 0 {
return readError{ERR_COULD_NOT_PARSE, line}
return ReadError{ERR_COULD_NOT_PARSE, line}
}
key = strings.TrimSpace(line[0:i])
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (c *ConfigFile) read(reader io.Reader) (err error) {
qLen := len(valQuote)
pos := strings.LastIndex(lineRight[qLen:], valQuote)
if pos == -1 {
return readError{ERR_COULD_NOT_PARSE, line}
return ReadError{ERR_COULD_NOT_PARSE, line}
}
pos = pos + qLen
value = lineRight[qLen:pos]
Expand Down Expand Up @@ -276,14 +276,14 @@ func (c *ConfigFile) AppendFiles(files ...string) error {
return c.Reload()
}

// readError occurs when read configuration file with wrong format.
type readError struct {
// ReadError occurs when read configuration file with wrong format.
type ReadError struct {
Reason ParseError
Content string // Line content
}

// Error implement Error interface.
func (err readError) Error() string {
func (err ReadError) Error() string {
switch err.Reason {
case ERR_BLANK_SECTION_NAME:
return "empty section name not allowed"
Expand Down

0 comments on commit 3956362

Please sign in to comment.