Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
a4e8c85b
Commit
a4e8c85b
authored
Dec 04, 2017
by
keks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
legacy: correctly handle errors
parent
81504552
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
7 deletions
+28
-7
command.go
command.go
+7
-2
legacy.go
legacy.go
+16
-5
legacy_test.go
legacy_test.go
+5
-0
No files found.
command.go
View file @
a4e8c85b
...
...
@@ -70,9 +70,14 @@ var ErrIncorrectType = errors.New("The command returned a value with a different
// Call invokes the command for the given Request
func
(
c
*
Command
)
Call
(
req
Request
,
re
ResponseEmitter
)
(
err
error
)
{
// we need the named return parameter so we can change the value from defer()
defer
re
.
Close
()
defer
func
()
{
if
err
==
nil
{
re
.
Close
()
}
}()
cmd
,
err
:=
c
.
Get
(
req
.
Path
())
var
cmd
*
Command
cmd
,
err
=
c
.
Get
(
req
.
Path
())
if
err
!=
nil
{
return
err
}
...
...
legacy.go
View file @
a4e8c85b
...
...
@@ -63,8 +63,7 @@ func (rw *responseWrapper) Output() interface{} {
if
err
==
io
.
EOF
||
err
==
context
.
Canceled
{
return
}
if
err
!=
nil
{
}
else
if
err
!=
nil
{
log
.
Error
(
err
)
return
}
...
...
@@ -403,9 +402,21 @@ func NewCommand(oldcmd *oldcmds.Command) *Command {
errCh
:=
make
(
chan
error
)
go
res
.
Send
(
errCh
)
oldcmd
.
Run
(
oldReq
,
res
)
err
:=
<-
errCh
if
err
!=
nil
{
log
.
Error
(
err
)
select
{
case
err
:=
<-
errCh
:
if
err
!=
nil
{
if
e
,
ok
:=
err
.
(
*
cmdkit
.
Error
);
ok
{
err
=
*
e
}
if
e
,
ok
:=
err
.
(
cmdkit
.
Error
);
ok
{
re
.
SetError
(
e
.
Message
,
e
.
Code
)
}
else
{
re
.
SetError
(
err
.
Error
(),
cmdkit
.
ErrNormal
)
}
}
case
<-
req
.
Context
()
.
Done
()
:
re
.
SetError
(
req
.
Context
()
.
Err
(),
cmdkit
.
ErrNormal
)
}
}
}
...
...
legacy_test.go
View file @
a4e8c85b
...
...
@@ -2,6 +2,7 @@ package cmds
import
(
"bytes"
"context"
"fmt"
"io"
"testing"
...
...
@@ -57,6 +58,10 @@ func TestNewCommand(t *testing.T) {
t
.
Fatal
(
err
)
}
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
req
.
SetRootContext
(
ctx
)
buf
:=
bytes
.
NewBuffer
(
nil
)
testCmd
:=
root
.
Subcommand
(
"test"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment