Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
xxdk-wasm
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
xxdk-wasm
Commits
7e27bf72
Commit
7e27bf72
authored
2 years ago
by
Jono Wenger
Browse files
Options
Downloads
Patches
Plain Diff
Move errors to own file
parent
6663cc6a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!60
Revert "Fail a test to be sure it works"
,
!5
XX-4050 / SendE2e test
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
utils/errors.go
+70
-0
70 additions, 0 deletions
utils/errors.go
utils/errors_test.go
+33
-0
33 additions, 0 deletions
utils/errors_test.go
utils/utils.go
+4
-63
4 additions, 63 deletions
utils/utils.go
with
107 additions
and
63 deletions
utils/errors.go
0 → 100644
+
70
−
0
View file @
7e27bf72
////////////////////////////////////////////////////////////////////////////////
// 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"
)
This diff is collapsed.
Click to expand it.
utils/errors_test.go
0 → 100644
+
33
−
0
View file @
7e27bf72
////////////////////////////////////////////////////////////////////////////////
// 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
)
{
}
This diff is collapsed.
Click to expand it.
utils/utils.go
+
4
−
63
View file @
7e27bf72
...
...
@@ -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
p
romise
Constructor
.
New
(
handler
)
return
P
romise
.
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"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment