Skip to content

Commit 4e89918

Browse files
authored
Merge pull request #2 from factorio-item-browser/feature/better-error-messages
feature/better-error-messages
2 parents c74123d + aa43a55 commit 4e89918

23 files changed

+872
-99
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 1.1.0 - 2020-12-02
4+
5+
### Changed
6+
7+
- Moved output in case of an error to stderr, so stdout stays empty if no final image can be rendered.
8+
- Error message now contains more details, e.g. which file caused it.
9+
310
## 1.0.0 - 2020-04-24
411

512
- Initial release of the icon renderer, as port of the PHP implementation.

cmd/render-icon/render-icon.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
5-
"github.com/factorio-item-browser/export-icon-renderer/pkg/command"
66
"github.com/factorio-item-browser/export-icon-renderer/pkg/env"
7+
"github.com/factorio-item-browser/export-icon-renderer/pkg/log"
8+
"github.com/factorio-item-browser/export-icon-renderer/pkg/render"
9+
"github.com/factorio-item-browser/export-icon-renderer/pkg/transfer"
710
"os"
811
)
912

1013
const usage = `Factorio Item Browser - Export Icon Renderer
1114
Version: %s
1215
13-
Usage: render-icon icon_definition
14-
icon_definition: The JSON string defining the icon and its layers to render. This string must represent an Icon of
15-
the factorio-item-browser/export-data PHP package.
16+
Usage: render-icon <icon_definition>
17+
<icon_definition>: The JSON string defining the icon and its layers to render. This string must represent an Icon
18+
of the factorio-item-browser/export-data PHP package.
1619
1720
Environment variables:
1821
FACTORIO_DATA_DIRECTORY: The data directory of the Factorio game, containing the base and core mods.
@@ -22,17 +25,24 @@ Environment variables:
2225

2326
func main() {
2427
if len(os.Args) < 2 {
25-
fmt.Printf("Missing icon definition as first argument.\n\n")
26-
fmt.Printf(usage, env.Version)
28+
logger := log.Logger()
29+
logger.Error().Msg("Missing icon definition as first argument.")
30+
logger.Info().Msgf(usage, env.Version)
2731
os.Exit(1)
2832
}
2933

30-
cmd := command.NewRenderIcon()
31-
img, err := cmd.Run(os.Args[1])
34+
var icon transfer.Icon
35+
err := json.Unmarshal([]byte(os.Args[1]), &icon)
3236
if err != nil {
33-
fmt.Printf("Failed to render icon: %s\n", err)
37+
log.Error(err)
3438
os.Exit(1)
3539
}
3640

37-
fmt.Print(img)
41+
renderer := render.NewRenderer()
42+
image, err := renderer.Render(icon)
43+
if err != nil {
44+
log.Error(err)
45+
os.Exit(1)
46+
}
47+
fmt.Print(image)
3848
}

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/factorio-item-browser/export-icon-renderer
33
go 1.15
44

55
require (
6-
github.com/anthonynsimon/bild v0.11.2-0.20200422162521-c58fb61a4c52
7-
github.com/stretchr/testify v1.2.2
6+
github.com/anthonynsimon/bild v0.13.0
7+
github.com/rs/zerolog v1.20.0
8+
github.com/stretchr/testify v1.6.1
89
)

go.sum

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2-
github.com/anthonynsimon/bild v0.11.2-0.20200422162521-c58fb61a4c52 h1:FTLTizjeQCP9Rwv/zEIlEqj8dXEaHpIkgPDpZ0/IvwE=
3-
github.com/anthonynsimon/bild v0.11.2-0.20200422162521-c58fb61a4c52/go.mod h1:tpzzp0aYkAsMi1zmfhimaDyX1xjn2OUc1AJZK/TF0AE=
2+
github.com/anthonynsimon/bild v0.13.0 h1:mN3tMaNds1wBWi1BrJq0ipDBhpkooYfu7ZFSMhXt1C8=
3+
github.com/anthonynsimon/bild v0.13.0/go.mod h1:tpzzp0aYkAsMi1zmfhimaDyX1xjn2OUc1AJZK/TF0AE=
44
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
55
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
66
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
77
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
8+
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
89
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
10+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
911
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1012
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1113
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
@@ -16,8 +18,12 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
1618
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
1719
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
1820
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
21+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1922
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2023
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
24+
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
25+
github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
26+
github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
2127
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
2228
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
2329
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
@@ -27,14 +33,27 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
2733
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
2834
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
2935
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
36+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
37+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3038
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3139
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
40+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
41+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3242
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
3343
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
3444
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
45+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3546
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9 h1:uc17S921SPw5F2gJo7slQ3aqvr2RwpL7eb3+DZncu3s=
3647
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
48+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
49+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3750
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
51+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3852
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
53+
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
54+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
55+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3956
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4057
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
58+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
59+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/command/rendericon.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

pkg/errors/image.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package errors
2+
3+
import "fmt"
4+
5+
type InvalidFileNameError struct {
6+
fileName string
7+
}
8+
9+
func NewInvalidFileNameError(fileName string) error {
10+
return &InvalidFileNameError{
11+
fileName: fileName,
12+
}
13+
}
14+
15+
func (e *InvalidFileNameError) Error() string {
16+
return "unable to parse file name"
17+
}
18+
19+
func (e *InvalidFileNameError) LogFields() map[string]interface{} {
20+
return map[string]interface{}{
21+
"file": e.fileName,
22+
}
23+
}
24+
25+
type ImageLoadError struct {
26+
fileName string
27+
originalError error
28+
}
29+
30+
func NewImageLoadError(fileName string, originalError error) error {
31+
return &ImageLoadError{
32+
fileName: fileName,
33+
originalError: originalError,
34+
}
35+
}
36+
37+
func (e *ImageLoadError) Error() string {
38+
return fmt.Sprintf("unable to load image: %s", e.originalError)
39+
}
40+
41+
func (e *ImageLoadError) Unwrap() error {
42+
return e.originalError
43+
}
44+
45+
func (e *ImageLoadError) LogFields() map[string]interface{} {
46+
return map[string]interface{}{
47+
"file": e.fileName,
48+
}
49+
}
50+
51+
type EncodingError struct {
52+
originalError error
53+
}
54+
55+
func NewEncodingError(originalError error) error {
56+
return &EncodingError{
57+
originalError: originalError,
58+
}
59+
}
60+
61+
func (e *EncodingError) Error() string {
62+
return fmt.Sprintf("unable to encode image: %s", e.originalError)
63+
}
64+
65+
func (e *EncodingError) Unwrap() error {
66+
return e.originalError
67+
}

pkg/errors/image_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package errors
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/stretchr/testify/assert"
7+
"testing"
8+
)
9+
10+
func TestInvalidFileNameError(t *testing.T) {
11+
fileName := "abc"
12+
expectedMessage := "unable to parse file name"
13+
expectedLogFields := map[string]interface{}{
14+
"file": "abc",
15+
}
16+
17+
err := NewInvalidFileNameError(fileName)
18+
19+
assert.IsType(t, &InvalidFileNameError{}, err)
20+
assert.Exactly(t, expectedMessage, err.Error())
21+
assert.Exactly(t, expectedLogFields, err.(*InvalidFileNameError).LogFields())
22+
}
23+
24+
func TestImageLoadError(t *testing.T) {
25+
fileName := "abc"
26+
originalError := fmt.Errorf("test error")
27+
expectedMessage := "unable to load image: test error"
28+
expectedLogFields := map[string]interface{}{
29+
"file": "abc",
30+
}
31+
32+
err := NewImageLoadError(fileName, originalError)
33+
34+
assert.IsType(t, &ImageLoadError{}, err)
35+
assert.Exactly(t, expectedMessage, err.Error())
36+
assert.Exactly(t, originalError, errors.Unwrap(err))
37+
assert.Exactly(t, expectedLogFields, err.(*ImageLoadError).LogFields())
38+
}
39+
40+
func TestEncodingError(t *testing.T) {
41+
originalError := fmt.Errorf("test error")
42+
expectedMessage := "unable to encode image: test error"
43+
44+
err := NewEncodingError(originalError)
45+
46+
assert.IsType(t, &EncodingError{}, err)
47+
assert.Exactly(t, expectedMessage, err.Error())
48+
assert.Exactly(t, originalError, errors.Unwrap(err))
49+
}

pkg/errors/render.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package errors
2+
3+
import "fmt"
4+
5+
type RenderIconError struct {
6+
iconId string
7+
originalError error
8+
}
9+
10+
func NewRenderIconError(iconId string, originalError error) error {
11+
return &RenderIconError{
12+
iconId: iconId,
13+
originalError: originalError,
14+
}
15+
}
16+
17+
func (e *RenderIconError) Error() string {
18+
return fmt.Sprintf("render icon error: %s", e.originalError)
19+
}
20+
21+
func (e *RenderIconError) Unwrap() error {
22+
return e.originalError
23+
}
24+
25+
func (e *RenderIconError) LogFields() map[string]interface{} {
26+
return map[string]interface{}{
27+
"icon": e.iconId,
28+
}
29+
}

pkg/errors/render_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package errors
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/stretchr/testify/assert"
7+
"testing"
8+
)
9+
10+
func TestRenderIconError(t *testing.T) {
11+
iconId := "abc"
12+
originalError := fmt.Errorf("test error")
13+
expectedMessage := "render icon error: test error"
14+
expectedLogFields := map[string]interface{}{
15+
"icon": "abc",
16+
}
17+
18+
err := NewRenderIconError(iconId, originalError)
19+
20+
assert.IsType(t, &RenderIconError{}, err)
21+
assert.Exactly(t, expectedMessage, err.Error())
22+
assert.Exactly(t, originalError, errors.Unwrap(err))
23+
assert.Exactly(t, expectedLogFields, err.(*RenderIconError).LogFields())
24+
}

0 commit comments

Comments
 (0)