Skip to content

Commit

Permalink
Merge pull request #14 from spiegel-im-spiegel/new-error-handling
Browse files Browse the repository at this point in the history
Add new error handler
  • Loading branch information
spiegel-im-spiegel authored Mar 2, 2019
2 parents b3de146 + 80c6264 commit 6a4538a
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- "1.11.x"
- "1.12"

env:
global:
Expand Down
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@

See [go.mod](https://github.com/spiegel-im-spiegel/mklink/blob/master/go.mod) file.

### Module Requirement Graph

```
$ go mod graph
github.com/spiegel-im-spiegel/mklink github.com/PuerkitoBio/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/atotto/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/inconshreveable/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/mattn/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/pkg/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/spf13/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/spf13/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/spiegel-im-spiegel/[email protected]
github.com/spiegel-im-spiegel/mklink golang.org/x/[email protected]
github.com/spiegel-im-spiegel/mklink golang.org/x/[email protected]
github.com/spiegel-im-spiegel/[email protected] github.com/mattn/[email protected]
github.com/PuerkitoBio/[email protected] github.com/andybalholm/[email protected]
github.com/PuerkitoBio/[email protected] golang.org/x/[email protected]
github.com/andybalholm/[email protected] golang.org/x/[email protected]
```

## Usage

```go
Expand Down
4 changes: 1 addition & 3 deletions cli/mklink/facade/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
}
_ = ui.Outputln()
}
if err := scanner.Err(); err != nil {
return err
}
return scanner.Err()
}
return nil
},
Expand Down
14 changes: 14 additions & 0 deletions cli/mklink/facade/facade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ package facade

import (
"bytes"
"fmt"
"testing"

"github.com/spiegel-im-spiegel/gocli/exitcode"
"github.com/spiegel-im-spiegel/gocli/rwi"
)

func TestInteractiveError(t *testing.T) {
outBuf := new(bytes.Buffer)
outErrBuf := new(bytes.Buffer)
ui := rwi.New(rwi.WithWriter(outBuf), rwi.WithErrorWriter(outErrBuf))
args := []string{"-i"}

exit := Execute(ui, args)
if exit == exitcode.Normal {
t.Errorf("Execute(markdown) = \"%v\", want \"%v\".", exit, exitcode.Abnormal)
}
fmt.Printf("Info: %v", outErrBuf.String())
}

func TestStyleMarkdown(t *testing.T) {
outBuf := new(bytes.Buffer)
outErrBuf := new(bytes.Buffer)
Expand Down
14 changes: 6 additions & 8 deletions cli/mklink/facade/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"io"

"github.com/atotto/clipboard"
"github.com/pkg/errors"
"github.com/spiegel-im-spiegel/gocli/prompt"
"github.com/spiegel-im-spiegel/gocli/rwi"
"github.com/spiegel-im-spiegel/mklink/cli/mklink/makelink"
"github.com/spiegel-im-spiegel/mklink/errs"
errors "golang.org/x/xerrors"
)

func interactiveMode(ui *rwi.RWI, cxt *makelink.Context) error {
Expand All @@ -23,25 +24,22 @@ func interactiveMode(ui *rwi.RWI, cxt *makelink.Context) error {
}
r, err := cxt.MakeLink(url)
if err != nil {
return err.Error(), nil
return errs.Cause(err).Error(), nil
}
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, r); err != nil {
return "", err
return "", errs.Wrap(err, "error when output result")
}
res := buf.String()
return res, clipboard.WriteAll(res)
return res, errs.Wrap(clipboard.WriteAll(res), "error when output result")
},
prompt.WithPromptString("mklink> "),
prompt.WithHeaderMessage("Input 'q' or 'quit' to stop"),
)
if !p.IsTerminal() {
return errors.New("not terminal (or pipe?)")
}
if err := p.Run(); err != nil {
return err
}
return nil
return errs.Wrap(p.Run(), "error in interactive mode")
}

/* Copyright 2019 Spiegel
Expand Down
8 changes: 4 additions & 4 deletions cli/mklink/makelink/makelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"io"

"github.com/pkg/errors"
"github.com/spiegel-im-spiegel/mklink"
"github.com/spiegel-im-spiegel/mklink/errs"
)

//Context class is context for making link
Expand All @@ -23,11 +23,11 @@ func New(s mklink.Style, log io.Writer) *Context {
//MakeLink is making link
func (c *Context) MakeLink(url string) (io.Reader, error) {
if c == nil {
return nil, errors.New("nil pointer in makelink.Context.MakeLink() function")
return nil, errs.Wrap(errs.ErrNullPointer, "reference error")
}
lnk, err := mklink.New(url)
if err != nil {
return nil, err
return nil, errs.Wrap(err, "error in constructor")
}

rRes := lnk.Encode(c.linkStyle)
Expand All @@ -36,7 +36,7 @@ func (c *Context) MakeLink(url string) (io.Reader, error) {
}
buf := new(bytes.Buffer)
if _, err := io.Copy(c.log, io.TeeReader(rRes, buf)); err != nil {
return buf, err
return buf, errs.Wrap(err, "error in logging")
}
fmt.Fprintln(c.log) //new line in logfile
return buf, nil
Expand Down
12 changes: 8 additions & 4 deletions cli/mklink/makelink/makelink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package makelink

import (
"bytes"
"fmt"
"io"
"os"
"testing"

"github.com/spiegel-im-spiegel/mklink"
Expand All @@ -12,11 +14,11 @@ func TestMakeLink(t *testing.T) {
logBuf := new(bytes.Buffer)
rRes, err := New(mklink.StyleMarkdown, logBuf).MakeLink("https://git.io/vFR5M")
if err != nil {
t.Errorf("Error of Context.MakeLink() = %v, want nil.", err)
t.Errorf("Error in Context.MakeLink(): %+v", err)
}
outBuf := new(bytes.Buffer)
if _, err := io.Copy(outBuf, rRes); err != nil {
t.Errorf("Error of io.Copy() = %v, want nil.", err)
t.Errorf("Error in io.Copy(): %+v", err)
}

res := "[GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink)"
Expand All @@ -33,11 +35,11 @@ func TestMakeLink(t *testing.T) {
func TestMakeLinkNil(t *testing.T) {
rRes, err := New(mklink.StyleMarkdown, nil).MakeLink("https://git.io/vFR5M")
if err != nil {
t.Errorf("Context.MakeLink() = \"%v\", want nil error.", err)
t.Errorf("Error in Context.MakeLink(): %+v", err)
}
outBuf := new(bytes.Buffer)
if _, err := io.Copy(outBuf, rRes); err != nil {
t.Errorf("Error of io.Copy() = %v, want nil.", err)
t.Errorf("Error in io.Copy(): %+v", err)
}

res := "[GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink)"
Expand All @@ -51,6 +53,8 @@ func TestMakeLinkErr(t *testing.T) {
_, err := New(mklink.StyleMarkdown, nil).MakeLink("https://foo.bar")
if err == nil {
t.Error("Context.MakeLink() = nil error, not want nil error.")
} else {
fmt.Fprintf(os.Stderr, "info: %+v\n", err)
}
}

Expand Down
30 changes: 30 additions & 0 deletions errs/cause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package errs

import (
errors "golang.org/x/xerrors"
)

func Cause(err error) error {
for {
unwrap := errors.Unwrap(err)
if unwrap == nil {
return err
}
err = unwrap
}
}

/* Copyright 2019 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
38 changes: 38 additions & 0 deletions errs/cause_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package errs

import (
"testing"
)

func TestCause(t *testing.T) {
testCases := []struct {
err error
cause error
}{
{err: nil, cause: nil},
{err: ErrNoImplement, cause: ErrNoImplement},
{err: Wrap(ErrNoImplement, "wrapping error"), cause: ErrNoImplement},
}

for _, tc := range testCases {
res := Cause(tc.err)
if res != tc.cause {
t.Errorf("Cause in \"%v\" == \"%v\", want \"%v\"", tc.err, res, tc.cause)
}
}
}

/* Copyright 2019 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
38 changes: 38 additions & 0 deletions errs/errs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package errs

import "fmt"

//Num is error number for CVSS
type Num int

const (
ErrNullPointer Num = iota + 1
ErrNoImplement
)

var errMessage = map[Num]string{
ErrNullPointer: "Null reference instance",
ErrNoImplement: "This style is not implementation",
}

func (n Num) Error() string {
if s, ok := errMessage[n]; ok {
return s
}
return fmt.Sprintf("unknown error (%d)", int(n))
}

/* Copyright 2019 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Loading

0 comments on commit 6a4538a

Please sign in to comment.