This is an automated email from the ASF dual-hosted git repository.

maxyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry-go-libs.git

commit 74173a5f7dc019d83b8c80987be9e2f32ef54e2c
Author: Jianwen Dong <[email protected]>
AuthorDate: Tue Oct 17 09:21:06 2023 -0700

    Add error formatting package
    
    - Also add "conv" to `show_coverage.sh`
    - Bump Ginkgo from v1 to v2 for consistency with the current testing
      code
    
    Co-authored-by: Jianwen Dong <[email protected]>
    Co-authored-by: Brandon Tat <[email protected]>
    Co-authored-by: Stu Pollock <[email protected]>
    Co-authored-by: Gaurab Dey <[email protected]>
---
 .gitignore              |  1 +
 Makefile                | 10 ++++++++-
 gperror/gperror.go      | 32 ++++++++++++++++++++++++++++
 gperror/gperror_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
 show_coverage.sh        |  2 +-
 5 files changed, 98 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index 3142232..b7a59fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
 *.o
 *.a
 *.so
+*.swp
 
 # Folders
 _obj
diff --git a/Makefile b/Makefile
index b288aa5..d5cf195 100644
--- a/Makefile
+++ b/Makefile
@@ -31,7 +31,15 @@ $(GINKGO):
        go install github.com/onsi/ginkgo/v2/ginkgo@latest
 
 unit: $(GINKGO)
-               ginkgo -r --keep-going --randomize-suites --randomize-all 
cluster dbconn gplog iohelper structmatcher conv 2>&1
+               ginkgo -r --keep-going --randomize-suites --randomize-all \
+                       cluster \
+                       conv \
+                       dbconn \
+                       gperror \
+                       gplog \
+                       iohelper \
+                       structmatcher \
+                       2>&1
 
 coverage :
                @./show_coverage.sh
diff --git a/gperror/gperror.go b/gperror/gperror.go
new file mode 100644
index 0000000..cda17de
--- /dev/null
+++ b/gperror/gperror.go
@@ -0,0 +1,32 @@
+package gperror
+
+import "fmt"
+
+type ErrorCode uint32
+
+type Error interface {
+       error
+       GetCode() ErrorCode
+       GetErr() error
+}
+
+type GpError struct {
+       Err error
+       ErrorCode
+}
+
+func (e *GpError) Error() string {
+       return fmt.Sprintf("ERROR[%04d] %s", e.GetCode(), e.Err.Error())
+}
+
+func (e *GpError) GetCode() ErrorCode {
+       return e.ErrorCode
+}
+
+func (e *GpError) GetErr() error {
+       return e.Err
+}
+
+func New(errorCode ErrorCode, errorFormat string, args ...any) Error {
+       return &GpError{ErrorCode: errorCode, Err: fmt.Errorf(errorFormat, 
args...)}
+}
diff --git a/gperror/gperror_test.go b/gperror/gperror_test.go
new file mode 100644
index 0000000..dbc78ab
--- /dev/null
+++ b/gperror/gperror_test.go
@@ -0,0 +1,55 @@
+package gperror_test
+
+import (
+       "errors"
+       "testing"
+
+       . "github.com/onsi/ginkgo/v2"
+       . "github.com/onsi/gomega"
+
+       "github.com/cloudberrydb/gp-common-go-libs/gperror"
+)
+
+func TestGpError(t *testing.T) {
+       RegisterFailHandler(Fail)
+       RunSpecs(t, "Gperror Suite")
+}
+
+var _ = Describe("gperror", func() {
+       var testErr *gperror.GpError
+
+       BeforeEach(func() {
+               testErr = &gperror.GpError{
+                       ErrorCode: gperror.ErrorCode(4321),
+                       Err:       errors.New("test-error"),
+               }
+       })
+
+       Describe("Error", func() {
+               It("returns a formatted string representation of the error", 
func() {
+                       Expect(testErr.Error()).To(Equal("ERROR[4321] 
test-error"))
+               })
+       })
+
+       Describe("GetCode", func() {
+               It("returns the error code", func() {
+                       
Expect(testErr.GetCode()).To(Equal(gperror.ErrorCode(4321)))
+               })
+       })
+
+       Describe("GetErr", func() {
+               It("returns a string representation of the embedded error", 
func() {
+                       
Expect(testErr.GetErr()).To(MatchError(errors.New("test-error")))
+               })
+       })
+
+       Describe("New", func() {
+               It("matches an independently created struct", func() {
+                       expectedErr := &gperror.GpError{
+                               ErrorCode: gperror.ErrorCode(9999),
+                               Err:       errors.New("unexpected error: some 
error"),
+                       }
+                       Expect(gperror.New(9999, "unexpected error: %s", "some 
error")).To(Equal(expectedErr))
+               })
+       })
+})
diff --git a/show_coverage.sh b/show_coverage.sh
index 10cd083..de14d0c 100755
--- a/show_coverage.sh
+++ b/show_coverage.sh
@@ -3,7 +3,7 @@
 DIR="github.com/cloudberrydb/gp-common-go-libs"
 RESULTS="/tmp/results.out"
 echo "mode: set" > /tmp/coverage.out # Need this line at the start of the file 
for the total coverage at the end
-for PACKAGE in "cluster" "dbconn" "gplog" "iohelper" "structmatcher"; do
+for PACKAGE in "cluster" "conv" "dbconn" "gperror" "gplog" "iohelper" 
"structmatcher"; do
   # Generate code coverage statistics for all packages, write the coverage 
statistics to a file, and print the coverage percentage to the shell
   go test -coverpkg "$DIR/$PACKAGE" "$DIR/$PACKAGE" 
-coverprofile="/tmp/unit_$PACKAGE.out" | awk '{printf("%s unit test 
coverage|%s", $2, $5)}' | awk -F"/" '{print $4}' >> $RESULTS
   # Filter out the first "mode: set" line from each coverage file and 
concatenate them all


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to