forked from hoisie/mustache
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As a library user, we needed a way to translate error messages coming from the library. This pull request introduces a way to let library users customize error messages if they want. It is as well fully retro-compatible as it doesn't change the current error messages. What the pull request changes: * Make the error type public * Add an error code to let library users identify the error reason * Add a `Reason` attribute for errors cases that need the name of the entity generating the error * Make the `Line`, `Code` and `Reason` attribute public to let library users customize (translate) error message * Keep the current error messages
- Loading branch information
Showing
3 changed files
with
108 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mustache | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ErrorCode is the list of allowed values for the error's code. | ||
type ErrorCode string | ||
|
||
// List of values that ErrorCode can take. | ||
const ( | ||
ErrUnmatchedOpenTag ErrorCode = "unmatched_open_tag" | ||
ErrEmptyTag ErrorCode = "empty_tag" | ||
ErrSectionNoClosingTag ErrorCode = "section_no_closing_tag" | ||
ErrInterleavedClosingTag ErrorCode = "interleaved_closing_tag" | ||
ErrInvalidMetaTag ErrorCode = "invalid_meta_tag" | ||
ErrUnmatchedCloseTag ErrorCode = "unmatched_close_tag" | ||
) | ||
|
||
// ParseError represents an error during the parsing | ||
type ParseError struct { | ||
// Line contains the line of the error | ||
Line int | ||
// Code contains the error code of the error | ||
Code ErrorCode | ||
// Reason contains the name of the element generating the error | ||
Reason string | ||
} | ||
|
||
func (e ParseError) Error() string { | ||
return fmt.Sprintf("line %d: %s", e.Line, e.defaultMessage()) | ||
} | ||
|
||
func (e ParseError) defaultMessage() string { | ||
switch e.Code { | ||
case ErrUnmatchedOpenTag: | ||
return "unmatched open tag" | ||
case ErrEmptyTag: | ||
return "empty tag" | ||
case ErrSectionNoClosingTag: | ||
return fmt.Sprintf("Section %s has no closing tag", e.Reason) | ||
case ErrInterleavedClosingTag: | ||
return fmt.Sprintf("interleaved closing tag: %s", e.Reason) | ||
case ErrInvalidMetaTag: | ||
return "Invalid meta tag" | ||
case ErrUnmatchedCloseTag: | ||
return "unmatched close tag" | ||
default: | ||
return "unknown error" | ||
} | ||
} | ||
|
||
func newError(line int, code ErrorCode) ParseError { | ||
return ParseError{ | ||
Line: line, | ||
Code: code, | ||
} | ||
} | ||
|
||
func newErrorWithReason(line int, code ErrorCode, reason string) ParseError { | ||
return ParseError{ | ||
Line: line, | ||
Code: code, | ||
Reason: reason, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters