From 7e27bf7263179f2cd98caacbd4f5188a80534461 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Fri, 9 Sep 2022 11:57:41 -0700
Subject: [PATCH] Move errors to own file

---
 utils/errors.go      | 70 ++++++++++++++++++++++++++++++++++++++++++++
 utils/errors_test.go | 33 +++++++++++++++++++++
 utils/utils.go       | 67 +++---------------------------------------
 3 files changed, 107 insertions(+), 63 deletions(-)
 create mode 100644 utils/errors.go
 create mode 100644 utils/errors_test.go

diff --git a/utils/errors.go b/utils/errors.go
new file mode 100644
index 00000000..b599a448
--- /dev/null
+++ b/utils/errors.go
@@ -0,0 +1,70 @@
+////////////////////////////////////////////////////////////////////////////////
+// Copyright © 2022 xx network SEZC                                           //
+//                                                                            //
+// Use of this source code is governed by a license that can be found in the  //
+// LICENSE file                                                               //
+////////////////////////////////////////////////////////////////////////////////
+
+package utils
+
+import (
+	"fmt"
+	"syscall/js"
+)
+
+// JsError converts the error to a Javascript Error.
+func JsError(err error) js.Value {
+	return Error.New(err.Error())
+}
+
+// JsTrace converts the error to a Javascript Error that includes the error's
+// stack trace.
+func JsTrace(err error) js.Value {
+	return Error.New(fmt.Sprintf("%+v", err))
+}
+
+// Throw function stub to throws Javascript exceptions. The exception must be
+// one of the defined Exception below. Any other error types will result in an
+// error.
+func Throw(exception Exception, err error) {
+	throw(exception, fmt.Sprintf("%+v", err))
+}
+
+func throw(exception Exception, message string)
+
+// Exception are the possible Javascript error types that can be thrown.
+type Exception string
+
+const (
+	// EvalError occurs when error has occurred in the eval() function.
+	//
+	// Deprecated: This exception is not thrown by JavaScript anymore, however
+	// the EvalError object remains for compatibility.
+	EvalError Exception = "EvalError"
+
+	// RangeError occurs when a numeric variable or parameter is outside its
+	// valid range.
+	RangeError Exception = "RangeError"
+
+	// ReferenceError occurs when a variable that does not exist (or hasn't yet
+	// been initialized) in the current scope is referenced.
+	ReferenceError Exception = "ReferenceError"
+
+	// SyntaxError occurs when trying to interpret syntactically invalid code.
+	SyntaxError Exception = "SyntaxError"
+
+	// TypeError occurs when an operation could not be performed, typically (but
+	// not exclusively) when a value is not of the expected type.
+	//
+	// A TypeError may be thrown when:
+	//
+	//  - an operand or argument passed to a function is incompatible with the
+	//    type expected by that operator or function; or
+	//  - when attempting to modify a value that cannot be changed; or
+	//  - when attempting to use a value in an inappropriate way.
+	TypeError Exception = "TypeError"
+
+	// URIError occurs when a global URI handling function was used in a wrong
+	// way.
+	URIError Exception = "URIError"
+)
diff --git a/utils/errors_test.go b/utils/errors_test.go
new file mode 100644
index 00000000..6f48e460
--- /dev/null
+++ b/utils/errors_test.go
@@ -0,0 +1,33 @@
+////////////////////////////////////////////////////////////////////////////////
+// Copyright © 2022 xx foundation                                             //
+//                                                                            //
+// Use of this source code is governed by a license that can be found in the  //
+// LICENSE file.                                                              //
+////////////////////////////////////////////////////////////////////////////////
+
+package utils
+
+import (
+	"github.com/pkg/errors"
+	"syscall/js"
+	"testing"
+)
+
+func TestJsError(t *testing.T) {
+	err := errors.Errorf("test error")
+
+	jsError := JsError(err)
+
+	t.Logf("%+v", jsError)
+	t.Logf("%+v", jsError.String())
+	t.Logf("%+v", js.Error{Value: jsError})
+}
+
+func TestJsTrace(t *testing.T) {
+}
+
+func TestThrow(t *testing.T) {
+}
+
+func Test_throw(t *testing.T) {
+}
diff --git a/utils/utils.go b/utils/utils.go
index 4c0323a1..1d7f9ca5 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -18,15 +18,15 @@ package utils
 
 import (
 	"encoding/json"
-	"fmt"
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
 	"syscall/js"
 )
 
 var (
-	promiseConstructor = js.Global().Get("Promise")
-	Uint8Array         = js.Global().Get("Uint8Array")
+	Error      = js.Global().Get("Error")
+	Promise    = js.Global().Get("Promise")
+	Uint8Array = js.Global().Get("Uint8Array")
 )
 
 // CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is
@@ -73,26 +73,6 @@ func JsonToJS(src []byte) js.Value {
 	return js.ValueOf(inInterface)
 }
 
-// JsError converts the error to a Javascript Error.
-func JsError(err error) js.Value {
-	errorConstructor := js.Global().Get("Error")
-	return errorConstructor.New(err.Error())
-}
-
-// JsTrace converts the error to a Javascript Error that includes the error's
-// stack trace.
-func JsTrace(err error) js.Value {
-	errorConstructor := js.Global().Get("Error")
-	return errorConstructor.New(fmt.Sprintf("%+v", err))
-}
-
-// Throw function stub to throws Javascript exceptions. The exception must be
-// one of the defined Exception below. Any other error types will result in an
-// error.
-func Throw(exception Exception, err error) {
-	throw(exception, fmt.Sprintf("%+v", err))
-}
-
 type PromiseFn func(resolve, reject func(args ...interface{}) js.Value)
 
 // CreatePromise creates a Javascript promise to return the value of a blocking
@@ -109,44 +89,5 @@ func CreatePromise(f PromiseFn) interface{} {
 	})
 
 	// Create and return the Promise object
-	return promiseConstructor.New(handler)
+	return Promise.New(handler)
 }
-
-func throw(exception Exception, message string)
-
-// Exception are the possible Javascript error types that can be thrown.
-type Exception string
-
-const (
-	// EvalError occurs when error has occurred in the eval() function.
-	//
-	// Deprecated: This exception is not thrown by JavaScript anymore, however
-	// the EvalError object remains for compatibility.
-	EvalError Exception = "EvalError"
-
-	// RangeError occurs when a numeric variable or parameter is outside its
-	// valid range.
-	RangeError Exception = "RangeError"
-
-	// ReferenceError occurs when a variable that does not exist (or hasn't yet
-	// been initialized) in the current scope is referenced.
-	ReferenceError Exception = "ReferenceError"
-
-	// SyntaxError occurs when trying to interpret syntactically invalid code.
-	SyntaxError Exception = "SyntaxError"
-
-	// TypeError occurs when an operation could not be performed, typically (but
-	// not exclusively) when a value is not of the expected type.
-	//
-	// A TypeError may be thrown when:
-	//
-	//  - an operand or argument passed to a function is incompatible with the
-	//    type expected by that operator or function; or
-	//  - when attempting to modify a value that cannot be changed; or
-	//  - when attempting to use a value in an inappropriate way.
-	TypeError Exception = "TypeError"
-
-	// URIError occurs when a global URI handling function was used in a wrong
-	// way.
-	URIError Exception = "URIError"
-)
-- 
GitLab