Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #4 from gnanderson/openSUSE

Gofixes and changes to build against current weekly/tip using new go install tool
  • Loading branch information...
commit 0ed5c21fcd8b90c511722e3965827e19d4b13092 2 parents 195cb2d + b74aa75
Stephan Kountso aka StepLg authored
66 erx/StringFormatter.go
View
@@ -0,0 +1,66 @@
+package erx
+
+import (
+ "fmt"
+ "io"
+ "strconv"
+
+ "strings"
+)
+
+func FormatConsole(w io.Writer, err Error, tab string) {
+ formatConsole_gen(w, err, tab, 0)
+}
+
+func formatConsole_gen(w io.Writer, err Error, tab string, level int) {
+ w.Write([]uint8(strings.Repeat(tab, level) + transformPath(err.File()) + ":"))
+ w.Write([]uint8(strconv.Itoa(err.Line()) + " " + err.Message() + "\n"))
+ funcFile, funcLine := err.Func().FileLine(err.Func().Entry())
+ w.Write([]uint8(strings.Repeat(tab, level) + transformPath(funcFile) + ":"))
+ w.Write([]uint8(strconv.Itoa(funcLine) + " " + err.Func().Name() + "\n"))
+ level++
+ if len(err.Variables()) > 0 {
+ w.Write([]uint8(strings.Repeat(tab, level) + "Scope variables:\n"))
+ for name, val := range err.Variables() {
+ w.Write([]uint8(strings.Repeat(tab, level+1) + name + "\t: "))
+ switch i := val.(type) {
+ case string:
+ w.Write([]uint8(i))
+ case fmt.Stringer:
+ w.Write([]uint8(i.String()))
+ default:
+ w.Write([]uint8(fmt.Sprint(i)))
+ }
+ w.Write([]uint8("\n"))
+ }
+ }
+ subErrs := err.Errors()
+ if len(subErrs) > 0 {
+ w.Write([]uint8(strings.Repeat(tab, level)))
+ w.Write([]uint8("Scope errors:\n"))
+ for _, subErr := range subErrs {
+ switch i := subErr.(type) {
+ case Error:
+ formatConsole_gen(w, i, tab, level+1)
+ case error:
+ w.Write([]uint8(strings.Repeat(tab, level+1)))
+ w.Write([]uint8(i.Error() + "\n"))
+ default:
+ w.Write([]uint8("???\n"))
+ }
+ }
+ }
+}
+
+// Cut from path first dirs
+func transformPath(path string) string {
+ // finding path in pathCuts to cut
+ for curPath := pathCuts.Front(); curPath != nil; curPath = curPath.Next() {
+ if pathStr, isString := curPath.Value.(string); isString {
+ if len(pathStr) <= len(path) && path[0:len(pathStr)] == pathStr {
+ return path[len(pathStr):]
+ }
+ }
+ }
+ return path
+}
87 erx/XMLFormatter.go
View
@@ -0,0 +1,87 @@
+package erx
+
+import (
+ "fmt"
+ "io"
+ "strconv"
+
+ "encoding/xml"
+ "strings"
+)
+
+/**
+ * err -- erx error
+ * indent -- flag to add indentation to result xml file
+ */
+func FormatSimpleXML(w io.Writer, err Error, indent bool) {
+ formatSimpleXML_gen(w, err, indent, 0)
+}
+
+func escape(w io.Writer, str string) {
+ xml.Escape(w, []uint8(str))
+}
+
+/**
+ *This function create XML format of error
+ *return string
+ */
+func formatSimpleXML_gen(w io.Writer, err Error, indent bool, level int) {
+ tab := ""
+ hyphen := ""
+ if indent {
+ tab = "\t"
+ hyphen = "\n"
+ }
+ if level == 0 {
+ w.Write([]uint8("<?xml version=\"1.0\" encoding=\"UTF-8\">" + hyphen))
+ }
+ w.Write([]uint8(strings.Repeat(tab, level) + "<error>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<message>"))
+ escape(w, err.Message())
+ w.Write([]uint8("</message>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<file>"))
+ escape(w, transformPath(err.File()))
+ w.Write([]uint8("</file>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<line>"))
+ escape(w, strconv.Itoa(err.Line()))
+ w.Write([]uint8("</line>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<variables>" + hyphen))
+ if len(err.Variables()) > 0 {
+ for name, val := range err.Variables() {
+ w.Write([]uint8(strings.Repeat(tab, level+2) + "<variable name=\""))
+ escape(w, name)
+ w.Write([]uint8("\">"))
+ switch i := val.(type) {
+ case string:
+ escape(w, i)
+ case fmt.Stringer:
+ escape(w, i.String())
+ default:
+ escape(w, fmt.Sprint(i))
+ }
+ w.Write([]uint8("</variable>" + hyphen))
+ }
+ }
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "</variables>" + hyphen))
+ subErrs := err.Errors()
+ if len(subErrs) > 0 {
+ w.Write([]uint8(strings.Repeat(tab, level)))
+ for _, subErr := range subErrs {
+ switch i := subErr.(type) {
+ case Error:
+ formatSimpleXML_gen(w, i, indent, level+1)
+ w.Write([]uint8(hyphen))
+ case error:
+ w.Write([]uint8(strings.Repeat(tab, level) + "<error type=\"2\">" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+2)))
+ escape(w, i.Error())
+ w.Write([]uint8(hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "</error>" + hyphen))
+ default:
+ w.Write([]uint8("???" + hyphen))
+ }
+ }
+ }
+
+ w.Write([]uint8(strings.Repeat(tab, level) + "</error>"))
+}
42 src/erx/erx.go → erx/erx.go
View
@@ -3,6 +3,7 @@ package erx
import (
"container/list"
"runtime"
+ "io"
)
@@ -17,9 +18,9 @@ func AddPathCut(path string) {
type Error interface {
Message() string
- Errors() *list.List
+ Errors() []interface{}
Variables() ErrorVariables
-
+
File() string
Line() int
Func() *runtime.Func
@@ -30,25 +31,25 @@ type Error interface {
type error_realization struct {
message string
-
+
file string
line int
- errors *list.List
+ errors *list.List
variables ErrorVariables
-
+
funcInfo *runtime.Func
}
func newErrorInitializer(level int) *error_realization {
- err := error_realization{"", "", 0, list.New(), make(map[string] interface{}), nil}
+ err := error_realization{"", "", 0, list.New(), make(map[string]interface{}), nil}
pc, file, line, ok := runtime.Caller(level)
if ok {
err.file, err.line = file, line
} else {
err.file, err.line = "???", 666
}
-
+
err.funcInfo = runtime.FuncForPC(pc)
return &err
}
@@ -66,15 +67,24 @@ func NewSequent(msg string, error interface{}) Error {
return Error(err)
}
+func AutoOutput(w io.Writer, outputType string, err Error) {
+ switch outputType {
+ case "XML":
+ FormatSimpleXML(w, err, true)
+ default:
+ FormatConsole(w, err, "\t")
+ }
+}
+
func NewSequentLevel(msg string, error interface{}, level int) Error {
- err := newErrorInitializer(level+2)
+ err := newErrorInitializer(level + 2)
err.message = msg
err.AddE(error)
return Error(err)
}
func (e *error_realization) Message() string {
- return e.message;
+ return e.message
}
func (e *error_realization) File() string {
@@ -89,8 +99,18 @@ func (e *error_realization) Func() *runtime.Func {
return e.funcInfo
}
-func (e *error_realization) Errors() *list.List {
- return e.errors
+func (e *error_realization) Errors() []interface{} {
+ len := 0
+ for item := e.errors.Front(); item != nil; item = item.Next() {
+ len += 1
+ }
+ res := make([]interface{}, len)
+ id := 0
+ for item := e.errors.Front(); item != nil; item = item.Next() {
+ res[id] = item.Value
+ id += 1
+ }
+ return res
}
func (e *error_realization) Variables() ErrorVariables {
6 examples/panic/Makefile
View
@@ -1,6 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=panic
-GOFILES=panic.go
-
-include $(GOROOT)/src/Make.cmd
59 examples/panic/panic.go
View
@@ -4,78 +4,89 @@ import (
"strings"
"strconv"
"os"
+ "io"
"bufio"
"fmt"
"runtime"
- "../../src/erx/_obj/erx"
+ "github.com/StepLg/go-erx/erx"
+ "flag"
)
func init() {
// add path prefix to erx cut list
_, file, _, _ := runtime.Caller(0)
index := strings.LastIndex(file, "/")
- if index!= -1 {
- dirName := file[0:strings.LastIndex(file, "/")+1]
+ if index != -1 {
+ dirName := file[0 : strings.LastIndex(file, "/")+1]
fmt.Println(dirName)
erx.AddPathCut(dirName)
}
+
}
func fileSum(fileName string) (result int) {
- makeError := func(err interface{}) (res erx.Error) {
- res = erx.NewSequentLevel("Sum integers from file.", err, 1)
- res.AddV("file name", fileName)
- return
- }
+ defer func() {
+ if err := recover(); err != nil {
+ res := erx.NewSequent("Sum integers from file.", err)
+ res.AddV("file name", fileName)
+ panic(res)
+ }
+ }()
result = 0
- f, err := os.OpenFile(fileName, os.O_RDONLY, 0000)
- if f==nil {
- panic(makeError(erx.NewSequent("Open file.", err)))
+ f, err := os.Open(fileName)
+ if f == nil {
+ panic(erx.NewSequent("Open file.", err))
}
-
+
reader := bufio.NewReader(f)
-
+
line, err := reader.ReadString('\n')
lineNum := 1
- for err==nil || err==os.EOF {
- if strings.TrimSpace(line)!="" {
+ for err == nil || err == io.EOF {
+ if strings.TrimSpace(line) != "" {
chunkNum := 1
for _, chunk := range strings.Split(line, " ") {
var curInt int
curInt, err = strconv.Atoi(strings.TrimSpace(chunk))
- if err!=nil {
+ if err != nil {
errErx := erx.NewSequent("Convert string to integer.", err)
errErx.AddV("chunk", chunk)
errErx.AddV("line num", lineNum)
errErx.AddV("chunk num", chunkNum)
- panic(makeError(errErx))
+ panic(errErx)
}
result += curInt
}
- }
+ }
- if err==os.EOF {
+ if err == io.EOF {
break
}
line, err = reader.ReadString('\n')
lineNum++
}
-
- if err!=os.EOF {
- panic(makeError(erx.NewSequent("Reading from file.", err)))
+
+ if err != io.EOF {
+ panic(erx.NewSequent("Reading from file.", err))
}
return
}
+func flagMode_get() string {
+ var mode *string = flag.String("mode", "console", "mode out: XML, console")
+ flag.Parse()
+ return *mode
+}
+
func main() {
defer func() {
if err := recover(); err != nil {
if errErx, ok := err.(erx.Error); ok {
- formatter := erx.NewStringFormatter(" ")
- fmt.Println(formatter.Format(errErx))
+ erx.AutoOutput(os.Stdout, flagMode_get(), errErx)
}
}
}()
+
fileSum("ints.txt")
}
6 examples/simple/Makefile
View
@@ -1,6 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=simple
-GOFILES=simple.go
-
-include $(GOROOT)/src/Make.cmd
19 examples/simple/simple.go
View
@@ -2,25 +2,25 @@ package main
import (
"fmt"
- "../../src/erx/_obj/erx"
+ "github.com/StepLg/go-erx/erx"
"os"
"runtime"
"strings"
)
type MyType struct {
- v int
+ v int
}
func (m *MyType) String() string {
- return "Hello error!"
+ return "Hello error!"
}
func init() {
_, file, _, _ := runtime.Caller(0)
index := strings.LastIndex(file, "/")
- if index!= -1 {
- dirName := file[0:strings.LastIndex(file, "/")+1]
+ if index != -1 {
+ dirName := file[0 : strings.LastIndex(file, "/")+1]
fmt.Println(dirName)
erx.AddPathCut(dirName)
}
@@ -32,8 +32,9 @@ func main() {
err := erx.NewSequent("Sequent error", osError)
err.AddV("var1", "444")
err.AddV("var2", &m)
- err1 := erx.NewSequent("Simple error", err)
- formatter := erx.NewStringFormatter(" ")
- fmt.Println(formatter.Format(err))
- fmt.Println(formatter.Format(err1))
+ erx.AutoOutput(os.Stdout, "XML", err)
+ //Other output witn inline print
+ fmt.Println("\n ------- \n\n")
+ err = erx.NewSequent("Simple error", err)
+ erx.AutoOutput(os.Stdout, "XML", err)
}
8 src/erx/Makefile
View
@@ -1,8 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=erx
-GOFILES=\
- erx.go\
- StringFormatter.go
-
-include $(GOROOT)/src/Make.pkg
83 src/erx/StringFormatter.go
View
@@ -1,83 +0,0 @@
-package erx
-
-import (
- "strings"
- "strconv"
- "fmt"
- "os"
-)
-
-type StringFormatter struct {
- indent string
-}
-
-func NewStringFormatter(indent string) *StringFormatter {
- formatter := new(StringFormatter)
- formatter.indent = indent
- return formatter
-}
-
-func (f *StringFormatter) Format(err Error) string {
- return f.formatLevel(err, 0)
-}
-
-func (f *StringFormatter) formatLevel(err Error, level int) string {
- result := ""
- result += strings.Repeat(f.indent, level)
- result += transformPath(err.File()) + ":" + strconv.Itoa(err.Line()) + " " + err.Message()
- result += "\n"
- result += strings.Repeat(f.indent, level)
- funcFile, funcLine := err.Func().FileLine(err.Func().Entry())
- result += transformPath(funcFile) + ":" + strconv.Itoa(funcLine) + " " + err.Func().Name()
- result += "\n"
- level++
- if len(err.Variables())>0 {
- result += strings.Repeat(f.indent, level)
- result += "Scope variables:\n"
- for name, val := range err.Variables() {
- result += strings.Repeat(f.indent, level+1)
- result += name + "\t: "
- switch i := val.(type) {
- case string :
- result += i
- case fmt.Stringer :
- result += i.String()
- default :
- result += fmt.Sprint(i)
- }
- result += "\n"
- }
- }
-
- curErr := err.Errors().Front()
- if curErr!=nil {
- result += strings.Repeat(f.indent, level)
- result += "Scope errors:\n"
- for curErr!=nil {
- switch i := curErr.Value.(type) {
- case Error :
- result += f.formatLevel(i, level+1)
- case os.Error :
- result += strings.Repeat(f.indent, level+1)
- result += i.String()
- default :
- result += "???\n"
- }
- curErr = curErr.Next()
- }
- }
- return result
-}
-
-// Cut from path first dirs
-func transformPath(path string) string {
- // finding path in pathCuts to cut
- for curPath := pathCuts.Front(); curPath!=nil; curPath = curPath.Next() {
- if pathStr, isString := curPath.Value.(string); isString {
- if len(pathStr)<=len(path) && path[0:len(pathStr)]==pathStr {
- return path[len(pathStr):]
- }
- }
- }
- return path
-}
Please sign in to comment.
Something went wrong with that request. Please try again.