Last active
April 8, 2019 17:58
-
-
Save edwardIshaq/bf8df49bb058de120fac68b846f23f92 to your computer and use it in GitHub Desktop.
Wrapping around Snappy error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SnappyJSON type implements json.Marshaler and represents a snappy compressed []byte | |
type SnappyJSON []byte | |
// SnappyError wrapper around the error returned from snappy | |
type SnappyError struct { | |
json SnappyJSON | |
err error | |
} | |
func (s *SnappyError) Error() string { | |
return fmt.Sprintf("SnappyJSON: Error compressing %v", s) | |
} | |
// UnmarshalJSON from json -> SnappyJSON | |
func (s *SnappyJSON) UnmarshalJSON(b []byte) error { | |
compressed, err := snappyCompress(b) | |
if err != nil { | |
return &SnappyError{b, err} | |
// return err | |
} | |
//Assign compressed to `s` | |
*s = compressed | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
inspired by https://blog.golang.org/error-handling-and-go