mirror of
https://github.com/clearlinux/clr-installer.git
synced 2026-04-28 11:13:46 +00:00
This patch introduces a new "specialized" error to deal with data model validation, with that we can distinguish errors and differently handle that situation. Signed-off-by: Leandro Dorileo <leandro.maciel.dorileo@intel.com>
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
// Copyright © 2018 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func testErrorf(t *testing.T) {
|
|
err := Errorf("traceable error")
|
|
|
|
e, ok := err.(TraceableError)
|
|
if !ok {
|
|
t.Fatal("Errorf() should return a TraceableError")
|
|
}
|
|
|
|
if e.Trace == "" {
|
|
t.Fatal("Traceable error should contain trace info")
|
|
}
|
|
|
|
if !strings.Contains(e.Error(), e.Trace) && strings.Contains(e.Error(), e.What) {
|
|
t.Fatal("Error() should return the content of Trace and What member")
|
|
}
|
|
}
|
|
|
|
func TestErrorf(t *testing.T) {
|
|
testErrorf(t)
|
|
}
|
|
|
|
func TestWrapp(t *testing.T) {
|
|
err := Wrap(fmt.Errorf("wrapper error"))
|
|
|
|
e, ok := err.(TraceableError)
|
|
if !ok {
|
|
t.Fatal("Wrap() should return a TraceableError")
|
|
}
|
|
|
|
if e.Trace == "" {
|
|
t.Fatal("Traceable error should contain trace info")
|
|
}
|
|
|
|
if !strings.Contains(e.Error(), e.Trace) && strings.Contains(e.Error(), e.What) {
|
|
t.Fatal("Error() should return the content of Trace and What member")
|
|
}
|
|
}
|
|
|
|
func TestValidationError(t *testing.T) {
|
|
msg := "Validation error"
|
|
ve := ValidationErrorf(msg)
|
|
|
|
if ve.Error() != msg {
|
|
t.Fatal("Wrong validation error message")
|
|
}
|
|
|
|
if !IsValidationError(ve) {
|
|
t.Fatal("IsValidationError() should report true")
|
|
}
|
|
|
|
te := Errorf("A traceable error")
|
|
if IsValidationError(te) {
|
|
t.Fatal("IsValidationError() should return false for a TraceableError")
|
|
}
|
|
}
|