From ad224a328ee4693048a42b7c78663f0e61832aba Mon Sep 17 00:00:00 2001 From: antanst Date: Mon, 12 May 2025 20:37:58 +0300 Subject: [PATCH] Change errors to use xerrors package. --- common/errors/errors.go | 41 ------------------- common/errors/errors_test.go | 38 ------------------ common/errors/hostError.go | 29 ++++++++++++++ common/errors/sentinelErrors.go | 8 ++++ common/workerStatus.go | 70 --------------------------------- config/errors.go | 14 ------- gopher/errors.go | 4 +- 7 files changed, 39 insertions(+), 165 deletions(-) delete mode 100644 common/errors/errors.go delete mode 100644 common/errors/errors_test.go create mode 100644 common/errors/hostError.go create mode 100644 common/errors/sentinelErrors.go delete mode 100644 common/workerStatus.go delete mode 100644 config/errors.go diff --git a/common/errors/errors.go b/common/errors/errors.go deleted file mode 100644 index bc306bc..0000000 --- a/common/errors/errors.go +++ /dev/null @@ -1,41 +0,0 @@ -package errors - -import ( - "fmt" - - "github.com/antanst/go_errors" -) - -// HostError is an error encountered while -// visiting a host, and should be recorded -// to the snapshot. -type HostError struct { - Err error -} - -func (e *HostError) Error() string { - return e.Err.Error() -} - -func (e *HostError) Unwrap() error { - return e.Err -} - -func NewHostError(err error) error { - return &HostError{Err: err} -} - -func IsHostError(err error) bool { - if err == nil { - return false - } - var asError *HostError - return go_errors.As(err, &asError) -} - -// Sentinel errors used for their string message primarily. -// Do not use them by themselves, to be embedded to HostError. -var ( - ErrBlacklistMatch = fmt.Errorf("black list match") - ErrRobotsMatch = fmt.Errorf("robots match") -) diff --git a/common/errors/errors_test.go b/common/errors/errors_test.go deleted file mode 100644 index b5026ec..0000000 --- a/common/errors/errors_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package errors_test - -import ( - "errors" - "fmt" - "testing" - - "gemini-grc/gemini" -) - -func TestErrGemini(t *testing.T) { - t.Parallel() - err := gemini.NewGeminiError(50, "50 server error") - if !errors.As(err, new(*gemini.GeminiError)) { - t.Errorf("TestErrGemini fail") - } -} - -func TestErrGeminiWrapped(t *testing.T) { - t.Parallel() - err := gemini.NewGeminiError(50, "50 server error") - errWrapped := fmt.Errorf("%w wrapped", err) - if !errors.As(errWrapped, new(*gemini.GeminiError)) { - t.Errorf("TestErrGeminiWrapped fail") - } -} - -func TestIsGeminiError(t *testing.T) { - t.Parallel() - err1 := gemini.NewGeminiError(50, "50 server error") - if !gemini.IsGeminiError(err1) { - t.Errorf("TestGeminiError fail #1") - } - wrappedErr1 := fmt.Errorf("wrapped %w", err1) - if !gemini.IsGeminiError(wrappedErr1) { - t.Errorf("TestGeminiError fail #2") - } -} diff --git a/common/errors/hostError.go b/common/errors/hostError.go new file mode 100644 index 0000000..cfc622c --- /dev/null +++ b/common/errors/hostError.go @@ -0,0 +1,29 @@ +package commonErrors + +import ( + "errors" + + "git.antanst.com/antanst/xerrors" +) + +type HostError struct { + xerrors.XError +} + +func IsHostError(err error) bool { + var temp *HostError + return errors.As(err, &temp) +} + +func NewHostError(err error) error { + xerr := xerrors.XError{ + UserMsg: "", + Code: 0, + Err: err, + IsFatal: false, + } + + return &HostError{ + xerr, + } +} diff --git a/common/errors/sentinelErrors.go b/common/errors/sentinelErrors.go new file mode 100644 index 0000000..4cb284c --- /dev/null +++ b/common/errors/sentinelErrors.go @@ -0,0 +1,8 @@ +package commonErrors + +import "fmt" + +var ( + ErrBlacklistMatch = fmt.Errorf("black list match") + ErrRobotsMatch = fmt.Errorf("robots match") +) diff --git a/common/workerStatus.go b/common/workerStatus.go deleted file mode 100644 index 2c3f6ef..0000000 --- a/common/workerStatus.go +++ /dev/null @@ -1,70 +0,0 @@ -package common - -import ( - "fmt" - "strings" - - "gemini-grc/config" -) - -type WorkerStatus struct { - ID int - Status string -} - -func UpdateWorkerStatus(workerID int, status string) { - if !config.GetConfig().PrintWorkerStatus { - return - } - if config.CONFIG.NumOfWorkers > 1 { - StatusChan <- WorkerStatus{ - ID: workerID, - Status: status, - } - } -} - -func PrintWorkerStatus(totalWorkers int, statusChan chan WorkerStatus) { - if !config.GetConfig().PrintWorkerStatus { - return - } - - // Create a slice to store current Status of each worker - statuses := make([]string, totalWorkers) - - // Initialize empty statuses - for i := range statuses { - statuses[i] = "" - } - - // Initial print - var output strings.Builder - // \033[H moves the cursor to the top left corner of the screen - // (ie, the first column of the first row in the screen). - // \033[J clears the part of the screen from the cursor to the end of the screen. - output.WriteString("\033[H\033[J") // Clear screen and move cursor to top - for i := range statuses { - output.WriteString(fmt.Sprintf("[%2d] \n", i)) - } - fmt.Print(output.String()) - - // Continuously receive Status updates - for update := range statusChan { - if update.ID >= totalWorkers { - continue - } - - // Update the Status - statuses[update.ID] = update.Status - - // Build the complete output string - output.Reset() - output.WriteString("\033[H\033[J") // Clear screen and move cursor to top - for i, status := range statuses { - output.WriteString(fmt.Sprintf("[%2d] %.100s\n", i, status)) - } - - // Print the entire Status - fmt.Print(output.String()) - } -} diff --git a/config/errors.go b/config/errors.go deleted file mode 100644 index 60482d7..0000000 --- a/config/errors.go +++ /dev/null @@ -1,14 +0,0 @@ -package config - -import "fmt" - -// ValidationError represents a config validation error -type ValidationError struct { - Param string - Value string - Reason string -} - -func (e ValidationError) Error() string { - return fmt.Sprintf("invalid value '%s' for %s: %s", e.Value, e.Param, e.Reason) -} diff --git a/gopher/errors.go b/gopher/errors.go index 75eddbe..edace9f 100644 --- a/gopher/errors.go +++ b/gopher/errors.go @@ -1,6 +1,6 @@ package gopher -import "github.com/antanst/go_errors" +import "errors" // GopherError is an error encountered while // visiting a Gopher host, and is only for @@ -26,5 +26,5 @@ func IsGopherError(err error) bool { return false } var asError *GopherError - return go_errors.As(err, &asError) + return errors.As(err, &asError) }