-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallpaper_error.go
More file actions
154 lines (130 loc) · 4.99 KB
/
Copy pathwallpaper_error.go
File metadata and controls
154 lines (130 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2025 Dídimo Grimaldo T.
* GoÜBRU
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Go Bulk Rename & Rename Workflow Utility
*-----------------------------------------------------------------*/
package carousel
import (
"fmt"
"runtime"
"strings"
)
/* ----------------------------------------------------------------
* G l o b a l s
*-----------------------------------------------------------------*/
const (
ErrMissingTarget ApplicationErrorCode = iota // @note update String()
ErrNoConfigurationDir
ErrNoQualifyingWallpaper
ErrUnknownCarousel
ErrUnknownCategory
ErrUnknownSessionManager
)
/* ----------------------------------------------------------------
* T y p e s
*-----------------------------------------------------------------*/
type ApplicationErrorCode int
type AppErrorBase[T ~int] struct {
errnum T
message string
location string
}
type BackgroundChangerError struct {
AppErrorBase[ApplicationErrorCode]
}
/* ----------------------------------------------------------------
* C o n s t r u c t o r s
*-----------------------------------------------------------------*/
func NewAppError(num ApplicationErrorCode, err error) *BackgroundChangerError {
return &BackgroundChangerError{AppErrorBase[ApplicationErrorCode]{num, err.Error(), getCallerFlex(2)}}
}
func NewAppErrorMsg(num ApplicationErrorCode, message string) *BackgroundChangerError {
return &BackgroundChangerError{AppErrorBase[ApplicationErrorCode]{num, message, getCallerFlex(2)}}
}
func NewAppErrorWith(num ApplicationErrorCode, message string, err error) *BackgroundChangerError {
return &BackgroundChangerError{AppErrorBase[ApplicationErrorCode]{num, fmt.Sprintf("%s: %s", message, err.Error()), getCallerFlex(2)}}
}
func NewAppErrorf(num ApplicationErrorCode, format string, v ...any) *BackgroundChangerError {
return &BackgroundChangerError{AppErrorBase[ApplicationErrorCode]{num, fmt.Sprintf(format, v...), getCallerFlex(2)}}
}
/*
func newAppError[T int](num T, err error) *AppErrorBase[T] {
return &AppErrorBase[T]{num, err.Error(), getCallerFlex(2)}
}
func newAppErrorMsg[T int](num T, message string) *AppErrorBase[T] {
return &AppErrorBase[T]{num, message, getCallerFlex(2)}
}
func newAppErrorWith[T int](num T, message string, err error) *AppErrorBase[T] {
return &AppErrorBase[T]{num, fmt.Sprintf("%s: %s", message, err.Error()), getCallerFlex(2)}
}
func newAppErrorf[T int](num T, format string, v ...any) *AppErrorBase[T] {
return &AppErrorBase[T]{num, fmt.Sprintf(format, v...), getCallerFlex(2)}
}
*/
/* ----------------------------------------------------------------
* P u b l i c M e t h o d s
*-----------------------------------------------------------------*/
/**
* Implement Error interface
*/
func (e *AppErrorBase[T]) Error() string {
t := fmt.Sprintf("%T", e.errnum)
if p := strings.Index(t, "."); p != -1 {
t = t[p:]
}
return fmt.Sprintf("(%s) #E%03d\n\t%s %s", t, e.errnum, e.location, e.message)
}
/**
* Set location of error.
* err := NewAesGcmError(14, "Bad thing happened").At("module")
*/
func (e *AppErrorBase[T]) At(location string) *AppErrorBase[T] {
e.location = location
return e
}
func (e *AppErrorBase[T]) Pretty() string {
return fmt.Sprintf("\t(Error)\n\tNumber: #E%03d\n\tMessage: %s\n\tLocation: %s\n", e.errnum, e.message, e.location)
}
func (n ApplicationErrorCode) String() string {
toString := map[ApplicationErrorCode]string{
ErrMissingTarget: "ErrMissingTarget",
ErrNoConfigurationDir: "ErrNoConfigurationDir",
ErrNoQualifyingWallpaper: "ErrNoQualifyingWallpaper",
ErrUnknownCarousel: "ErrUnknownCarousel",
ErrUnknownCategory: "ErrUnknownCategory",
ErrUnknownSessionManager: "ErrUnknownSessionManager",
}
return toString[n]
}
/* ----------------------------------------------------------------
* F u n c t i o n s
*-----------------------------------------------------------------*/
// @todo fix this output format
// @lordofscripts/bulkrename/flow.(*Workflow).recurseDirectory()#139
func getCallerFlex(stackIdx int) string {
// 3 works when test.Announce() is used
// 2 works when ShowCaseOK/Failed called directly from Test*()
pc, _, line, ok := runtime.Caller(stackIdx) // PC,file,line,ok
details := runtime.FuncForPC(pc)
if ok && details != nil {
info := details.Name()
lastSlash := strings.LastIndexByte(info, '/')
if lastSlash < 0 {
lastSlash = 0
}
lastDot := strings.LastIndexByte(info[lastSlash:], '.') + lastSlash
//fmt.Printf("INFO %s\n", info)
//fileS := filepath.Base(filename)
packageS := info[:lastDot] // in tests it returns 'command-line-*'
if strings.HasPrefix(packageS, "command-line") {
packageS = "main"
}
funcS := info[lastDot+1:]
return fmt.Sprintf("@%s.%s()#%d\n", packageS, funcS, line)
//fmt.Printf("called from %s\n", details.Name())
//return filename + "+" + string(line) + " " + packageS + ":" + funcS
}
return ""
}