 import Foundation
 import CWebP
 
-enum WebPEncodeError : Int, Error {
+// This is customised error that describes the pattern of error causes.
+// However, the error is unlikely to happen normally but it's still better to handle with throw-catch than fatal error.
+public enum WebPEncoderError: Error {
+    case invalidParameter
+    case versionMismatched
+}
+
+// This is the mapped error codes that CWebP.WebPEncode returns
+public enum WebPEncodeStatusCode: Int, Error {
     case ok = 0
     case outOfMemory           // memory error allocating objects
     case bitstreamOutOfMemory  // memory error while flushing bits

                        resizeWidth: Int = 0, resizeHeight: Int = 0) throws -> Data {
        var config = config.rawValue
        if WebPValidateConfig(&config) == 0 {
            fatalError("invalid config")
        }

        var picture = WebPPicture()
        if WebPPictureInit(&picture) == 0 {
            fatalError("version error")
        }

        picture.use_argb = config.lossless == 0 ? 0 : 1

        let ok = importer(&picture, dataPtr, Int32(stride))
        if ok == 0 {
            WebPPictureFree(&picture)
            throw WebPEncoderError.versionMismatched
        }

        if resizeHeight > 0 && resizeWidth > 0 {
            if (WebPPictureRescale(&picture, Int32(resizeWidth), Int32(resizeHeight)) == 0) {
                throw WebPEncodeStatusCode.outOfMemory
            }
        }

