Change errors to use xerrors package.

This commit is contained in:
2025-05-12 20:37:58 +03:00
parent a823f5abc3
commit ad224a328e
7 changed files with 39 additions and 165 deletions

View File

@@ -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")
)

View File

@@ -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")
}
}

View File

@@ -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,
}
}

View File

@@ -0,0 +1,8 @@
package commonErrors
import "fmt"
var (
ErrBlacklistMatch = fmt.Errorf("black list match")
ErrRobotsMatch = fmt.Errorf("robots match")
)

View File

@@ -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())
}
}

View File

@@ -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)
}

View File

@@ -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)
}