add config file parser

This commit is contained in:
2025-09-06 23:39:10 +04:00
parent c3292dee5f
commit 5cc13cfe7e
15 changed files with 1517 additions and 90 deletions

View File

@@ -63,6 +63,25 @@ func (e FieldParseErrors) Error() string {
return errorString
}
type UnmarshalError struct {
error error
}
func NewUnmarshalError(err error) UnmarshalError {
if err == nil {
err = ErrNoError
}
return UnmarshalError{err}
}
func (e UnmarshalError) Error() string {
return "Unmarshal error: " + e.error.Error()
}
func (e UnmarshalError) Unwrap() error {
return e.error
}
// ======================================== CLI ========================================
type CLIUnexpectedArgsError struct {

View File

@@ -205,20 +205,50 @@ func TestNewRemoteConfigFileParseError(t *testing.T) {
})
}
func TestErrorConstants(t *testing.T) {
t.Run("ErrNoError has correct message", func(t *testing.T) {
expected := "no error (internal)"
assert.Equal(t, expected, ErrNoError.Error())
func TestUnmarshalError_Error(t *testing.T) {
t.Run("Error returns formatted message", func(t *testing.T) {
originalErr := errors.New("yaml parsing failed")
err := NewUnmarshalError(originalErr)
expected := "Unmarshal error: yaml parsing failed"
assert.Equal(t, expected, err.Error())
})
t.Run("ErrCLINoArgs has correct message", func(t *testing.T) {
expected := "CLI expects arguments but received none"
assert.Equal(t, expected, ErrCLINoArgs.Error())
t.Run("Error with nil underlying error", func(t *testing.T) {
err := NewUnmarshalError(nil)
expected := "Unmarshal error: no error (internal)"
assert.Equal(t, expected, err.Error())
})
}
func TestUnmarshalError_Unwrap(t *testing.T) {
t.Run("Unwrap returns original error", func(t *testing.T) {
originalErr := errors.New("original error")
err := NewUnmarshalError(originalErr)
assert.Equal(t, originalErr, err.Unwrap())
})
t.Run("ErrCLIUnexpectedArgs has correct message", func(t *testing.T) {
expected := "CLI received unexpected arguments"
assert.Equal(t, expected, ErrCLIUnexpectedArgs.Error())
t.Run("Unwrap with nil error", func(t *testing.T) {
err := NewUnmarshalError(nil)
assert.Equal(t, ErrNoError, err.Unwrap())
})
}
func TestNewUnmarshalError(t *testing.T) {
t.Run("Creates UnmarshalError with correct values", func(t *testing.T) {
originalErr := errors.New("test error")
err := NewUnmarshalError(originalErr)
assert.Equal(t, originalErr, err.error)
})
t.Run("Creates UnmarshalError with ErrNoError when nil passed", func(t *testing.T) {
err := NewUnmarshalError(nil)
assert.Equal(t, ErrNoError, err.error)
})
}
@@ -242,4 +272,9 @@ func TestErrorImplementsErrorInterface(t *testing.T) {
var err error = NewRemoteConfigFileParseError(errors.New("test"))
assert.Error(t, err)
})
t.Run("UnmarshalError implements error interface", func(t *testing.T) {
var err error = NewUnmarshalError(errors.New("test"))
assert.Error(t, err)
})
}