You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
2.5 KiB
123 lines
2.5 KiB
|
2 years ago
|
// ------------------------------------------------------------------------
|
||
|
|
// 🚀 Project active3
|
||
|
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||
|
|
//
|
||
|
|
// File name active.go
|
||
|
|
// ✍️ Created by DEV
|
||
|
|
// 📅 Modified 04/05/2024
|
||
|
|
//
|
||
|
|
// 🔒 Copyright 2024 activething.com
|
||
|
|
//
|
||
|
|
// 📜 LICENSE --------------------------------------------------------------
|
||
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
|
// you may not use this file except in compliance with the License.
|
||
|
|
// You may obtain a copy of the License at
|
||
|
|
//
|
||
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
//
|
||
|
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
|
// See the License for the specific language governing permissions and
|
||
|
|
// limitations under the License.
|
||
|
|
// ------------------------------------------------------------------------
|
||
|
|
|
||
|
|
package erstr
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"typs/strs"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
|
||
|
|
labelError string = "error"
|
||
|
|
labelSep string = " : "
|
||
|
|
|
||
|
|
)
|
||
|
|
|
||
|
|
type (
|
||
|
|
|
||
|
|
stringer interface { String () string }
|
||
|
|
unwrapper interface { Unwrap () error }
|
||
|
|
|
||
|
|
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
var (
|
||
|
|
|
||
|
|
G = struct {
|
||
|
|
LabelSep string
|
||
|
|
LineLabelFmt string
|
||
|
|
LineErrorFmt string
|
||
|
|
|
||
|
|
Transformers strs.Transformers
|
||
|
|
}{
|
||
|
|
|
||
|
|
LabelSep : labelSep,
|
||
|
|
LineLabelFmt : "%s" + labelSep + "%s\n",
|
||
|
|
LineErrorFmt : labelError + labelSep + "%s\n",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
func StringifyError (text string, err error) string {
|
||
|
|
ln := []string { "", text }
|
||
|
|
|
||
|
|
var nl string
|
||
|
|
ne := err
|
||
|
|
for ne != nil {
|
||
|
|
if st,is := ne.(stringer); is {
|
||
|
|
nl = st.String()
|
||
|
|
}
|
||
|
|
if uw,is := ne.(unwrapper); is {
|
||
|
|
ne = uw.Unwrap()
|
||
|
|
} else {
|
||
|
|
ne = nil
|
||
|
|
if strings.Index(nl,labelError) != -1 {
|
||
|
|
ln[0]= G.Transformers.Transform(nl)
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ln = append(ln, G.Transformers.Transform(nl))
|
||
|
|
}
|
||
|
|
if len(ln[0]) == 0 {
|
||
|
|
ln[0]=G.Transformers.Transform(err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
return strings.Join(ln,"")
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
func FormatLineError(strErr string) string {
|
||
|
|
return fmt.Sprintf(G.LineErrorFmt,strErr) }
|
||
|
|
|
||
|
|
func FormatLine (label, text string) string {
|
||
|
|
return fmt.Sprintf(G.LineLabelFmt,label,text) }
|
||
|
|
|
||
|
|
|
||
|
|
func RemoveLabel (text string) string {
|
||
|
|
if strings.Index(text,G.LabelSep) == -1 {
|
||
|
|
return text
|
||
|
|
}
|
||
|
|
l := strings.Split(text,"\n")
|
||
|
|
b := bytes.Buffer{}
|
||
|
|
for _,t := range l {
|
||
|
|
if p := strings.Index(t,G.LabelSep); p != -1 {
|
||
|
|
b.WriteString(t[p+len(G.LabelSep):])
|
||
|
|
b.WriteByte('\n')
|
||
|
|
} else {
|
||
|
|
b.WriteString(t)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return b.String()
|
||
|
|
}
|
||
|
|
|