Skip to content

Commit 61c01ab

Browse files
authored
Merge pull request #3 from po3rin/improve_err_msg
improve err message
2 parents f7f540d + 06ca29c commit 61c01ab

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ Flags:
4444

4545
## Vim Plugin
4646

47-
VIm plugin version is here! this plugin lets you to run automatically when saved.
47+
Vim plugin version is here! this plugin lets you to run automatically when saved.
4848

4949
<a href="https://github.com/po3rin/vim-gofmtmd"><img src="https://github-link-card.s3.ap-northeast-1.amazonaws.com/po3rin/vim-gofmtmd.png" width="460px"></a>

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ require (
66
github.com/sergi/go-diff v1.0.0
77
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
88
github.com/spf13/cobra v0.0.5
9-
github.com/spf13/viper v1.3.2
109
gopkg.in/russross/blackfriday.v2 v2.0.0
1110
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
44
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
55
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
66
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
7+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
78
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
89
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
910
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
@@ -18,6 +19,7 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
1819
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
1920
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
2021
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
22+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2123
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2224
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
2325
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
@@ -36,6 +38,7 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
3638
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
3739
github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
3840
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
41+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3942
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4043
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
4144
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=

gofmtmd.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"fmt"
66
"go/format"
7+
"strconv"
8+
"strings"
79

810
"gopkg.in/russross/blackfriday.v2"
911
)
@@ -15,7 +17,7 @@ func FmtGoCodeInMarkdown(md []byte) ([]byte, error) {
1517
n := blackfriday.New(blackfriday.WithExtensions(blackfriday.FencedCode)).Parse(md)
1618
n.Walk(genFmtWalker(&md, &err))
1719
if err != nil {
18-
return nil, fmt.Errorf("[gofmtmd] failed to format go source code. Please check syntax : %v", err)
20+
return nil, fmt.Errorf("[gofmtmd] %w", err)
1921
}
2022
return md, nil
2123
}
@@ -29,7 +31,14 @@ func genFmtWalker(md *[]byte, fmterr *error) blackfriday.NodeVisitor {
2931
if isGoCodeBlock(node) {
3032
fmted, err := format.Source(node.Literal)
3133
if err != nil {
32-
*fmterr = err
34+
line := strings.Split(err.Error(), ":")[0]
35+
i, cvtErr := strconv.Atoi(line)
36+
if cvtErr != nil {
37+
*fmterr = fmt.Errorf("failed to parse error msg: %w", cvtErr)
38+
} else {
39+
code := bytes.Split(node.Literal, []byte("\n"))
40+
*fmterr = fmt.Errorf("line='%s', msg='%w'", string(code[i+1]), err)
41+
}
3342
return blackfriday.Terminate
3443
}
3544
*md = bytes.ReplaceAll(*md, bytes.TrimRight(node.Literal, "\n"), bytes.TrimRight(fmted, "\n"))

testdata/syntaxerror.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Syntax Error
2+
3+
this is syntax error
4+
5+
```go
6+
import "dddd"
7+
8+
funcmain(){}
9+
```

0 commit comments

Comments
 (0)