Skip to content

Commit 2e640c1

Browse files
authored
Merge pull request #85 from aldas/error_for_parse_not_supported
Add specific error type (ErrNotSupported) when parsing not supported sentence
2 parents df350fd + fdf5d11 commit 2e640c1

3 files changed

Lines changed: 33 additions & 22 deletions

File tree

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ func main() {
193193
panic(err)
194194
}
195195

196-
m, ok := s.(XYZType)
197-
if !ok {
198-
panic("Could not parse type XYZ")
196+
switch m := s.(type) {
197+
case XYZType:
198+
fmt.Printf("Raw sentence: %v\n", m)
199+
fmt.Printf("Time: %s\n", m.Time)
200+
fmt.Printf("Label: %s\n", m.Label)
201+
fmt.Printf("Counter: %d\n", m.Counter)
202+
fmt.Printf("Value: %f\n", m.Value)
203+
default:
204+
panic("Could not parse XYZ sentence")
199205
}
200-
201-
fmt.Printf("Raw sentence: %v\n", m)
202-
fmt.Printf("Time: %s\n", m.Time)
203-
fmt.Printf("Label: %s\n", m.Label)
204-
fmt.Printf("Counter: %d\n", m.Counter)
205-
fmt.Printf("Value: %f\n", m.Value)
206206
}
207207
```
208208

sentence.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ var (
2828
// ParserFunc callback used to parse specific sentence variants
2929
type ParserFunc func(BaseSentence) (Sentence, error)
3030

31+
// NotSupportedError is returned when parsed sentence is not supported
32+
type NotSupportedError struct {
33+
Prefix string
34+
}
35+
36+
// Error returns error message
37+
func (p *NotSupportedError) Error() string {
38+
return fmt.Sprintf("nmea: sentence prefix '%s' not supported", p.Prefix)
39+
}
40+
3141
// Sentence interface for all NMEA sentence
3242
type Sentence interface {
3343
fmt.Stringer
@@ -225,5 +235,5 @@ func Parse(raw string) (Sentence, error) {
225235
return newVDMVDO(s)
226236
}
227237
}
228-
return nil, fmt.Errorf("nmea: sentence prefix '%s' not supported", s.Prefix())
238+
return nil, &NotSupportedError{Prefix: s.Prefix()}
229239
}

sentence_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nmea
22

33
import (
4+
"errors"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -58,11 +59,11 @@ var sentencetests = []struct {
5859
},
5960
},
6061
{
61-
name: "valid NMEA 4.10 TAG Block",
62-
raw: "\\s:Satelite_1,c:1553390539*62\\!AIVDM,1,1,,A,13M@ah0025QdPDTCOl`K6`nV00Sv,0*52",
62+
name: "valid NMEA 4.10 TAG Block",
63+
raw: "\\s:Satelite_1,c:1553390539*62\\!AIVDM,1,1,,A,13M@ah0025QdPDTCOl`K6`nV00Sv,0*52",
6364
datatype: "VDM",
6465
talkerid: "AI",
65-
prefix: "AIVDM",
66+
prefix: "AIVDM",
6667
sent: BaseSentence{
6768
Talker: "AI",
6869
Type: "VDM",
@@ -108,17 +109,17 @@ var sentencetests = []struct {
108109
{
109110
name: "missing TAG Block start delimiter",
110111
raw: "s:Satelite_1,c:1553390539*62\\!AIVDM,1,1,,A,13M@ah0025QdPDTCOl`K6`nV00Sv,0*52",
111-
err: "nmea: sentence does not start with a '$' or '!'",
112+
err: "nmea: sentence does not start with a '$' or '!'",
112113
},
113114
{
114115
name: "missing TAG Block end delimiter",
115116
raw: "\\s:Satelite_1,c:1553390539*62!AIVDM,1,1,,A,13M@ah0025QdPDTCOl`K6`nV00Sv,0*52",
116-
err: "nmea: sentence does not start with a '$' or '!'",
117+
err: "nmea: sentence does not start with a '$' or '!'",
117118
},
118119
{
119120
name: "invalid TAG Block contents",
120121
raw: "\\\\!AIVDM,1,1,,A,13M@ah0025QdPDTCOl`K6`nV00Sv,0*52",
121-
err: "nmea: tagblock does not contain checksum separator",
122+
err: "nmea: tagblock does not contain checksum separator",
122123
},
123124
}
124125

@@ -191,32 +192,32 @@ func TestPrefix(t *testing.T) {
191192
var parsetests = []struct {
192193
name string
193194
raw string
194-
err string
195+
err error
195196
msg interface{}
196197
}{
197198
{
198199
name: "bad sentence",
199200
raw: "SDFSD,2340dfmswd",
200-
err: "nmea: sentence does not start with a '$' or '!'",
201+
err: errors.New("nmea: sentence does not start with a '$' or '!'"),
201202
},
202203
{
203204
name: "bad sentence type",
204205
raw: "$INVALID,123,123,*7D",
205-
err: "nmea: sentence prefix 'INVALID' not supported",
206+
err: &NotSupportedError{Prefix: "INVALID"},
206207
},
207208
{
208209
name: "bad encapsulated sentence type",
209210
raw: "!INVALID,1,2,*7E",
210-
err: "nmea: sentence prefix 'INVALID' not supported",
211+
err: &NotSupportedError{Prefix: "INVALID"},
211212
},
212213
}
213214

214215
func TestParse(t *testing.T) {
215216
for _, tt := range parsetests {
216217
t.Run(tt.name, func(t *testing.T) {
217218
m, err := Parse(tt.raw)
218-
if tt.err != "" {
219-
assert.EqualError(t, err, tt.err)
219+
if tt.err != nil {
220+
assert.Equal(t, err, tt.err)
220221
} else {
221222
assert.NoError(t, err)
222223
assert.Equal(t, tt.msg, m)

0 commit comments

Comments
 (0)