adrianmo / go-nmea

A NMEA parser library in pure Go
MIT License
227 stars 78 forks source link

Add specific error type (NotSupportedError) when parsing not supported sentence #85

Closed aldas closed 2 years ago

aldas commented 2 years ago

Add specific error type (NotSupportedError) when parsing not supported sentence so it would be easier to distinguish these kinds of cases from other parse errors (crc etc).

Example :

s, err := nmea.Parse(sentence)
if err != nil {
    var notSupportedErr *nmea.NotSupportedError
    if errors.As(err, &notSupportedErr) {
        log.Printf("Prefix %s is not supported\n", notSupportedErr.Prefix)
        continue // read next line
    }
    return err
}

For example I am testing stuff atm with Ublox GPS and it sends $GNTXT,01,01,02,FWVER=TIM 1.10*50 messages which trigger errors. I would like to have easy way to skip these lines (maybe log differently) but I am still interested in other parse problems - ala CRC errors.


I also modified one of the examples in README to show type switch syntax which is handy when you need specific code block of different sentence types.

icholy commented 2 years ago

Also btw, you don't need to make that err a pointer in your example:

s, err := nmea.Parse(sentence)
if err != nil {
    var nserr nmea.NotSupportedError
    if errors.As(err, &nserr) {
                log.Println(err)
        continue
    }
    return err
}