From f710c7ab8adb7df457e54e756ef762203d9bfd88 Mon Sep 17 00:00:00 2001 From: David Martos Date: Thu, 22 Aug 2019 11:38:09 +0200 Subject: [PATCH 1/3] Custom error codes on the plugin side --- plugin/method-channel.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/plugin/method-channel.go b/plugin/method-channel.go index 09c3d6fb..87398077 100644 --- a/plugin/method-channel.go +++ b/plugin/method-channel.go @@ -210,7 +210,16 @@ func (m *MethodChannel) handleMethodCall(handler MethodHandler, methodName strin reply, err := handler.HandleMethod(methodArgs) if err != nil { fmt.Printf("go-flutter: handler for method '%s' on channel '%s' returned an error: %v\n", methodName, m.channelName, err) - binaryReply, err := m.methodCodec.EncodeErrorEnvelope("error", err.Error(), nil) + + var errorCode string + switch t := err.(type) { + case *PluginError: + errorCode = t.code + default: + errorCode = "error" + } + + binaryReply, err := m.methodCodec.EncodeErrorEnvelope(errorCode, err.Error(), nil) if err != nil { fmt.Printf("go-flutter: failed to encode error envelope for method '%s' on channel '%s', error: %v\n", methodName, m.channelName, err) } @@ -223,3 +232,20 @@ func (m *MethodChannel) handleMethodCall(handler MethodHandler, methodName strin } responseSender.Send(binaryReply) } + +type PluginError struct { + err string + code string +} + +func (e *PluginError) Error() string { + return e.err +} + +func NewPluginError(code string, err error) (*PluginError) { + pe := &PluginError{ + code: code, + err: err.Error(), + } + return pe +} \ No newline at end of file From b05ece903cc68cbae4e98cce97e22e31cc61344c Mon Sep 17 00:00:00 2001 From: David Martos Date: Thu, 22 Aug 2019 12:40:35 +0200 Subject: [PATCH 2/3] docs --- plugin/method-channel.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/plugin/method-channel.go b/plugin/method-channel.go index 87398077..70bc2bb8 100644 --- a/plugin/method-channel.go +++ b/plugin/method-channel.go @@ -233,19 +233,24 @@ func (m *MethodChannel) handleMethodCall(handler MethodHandler, methodName strin responseSender.Send(binaryReply) } +// This error can be thrown from a go-flutter plugin. Useful if +// you are interested in returning custom error codes. +// If that is not the case, you can just throw normal Go 'error's type PluginError struct { - err string - code string + err string + code string } +// Needed to comply with the Golang 'error' interface func (e *PluginError) Error() string { - return e.err + return e.err } -func NewPluginError(code string, err error) (*PluginError) { +// Create an error with an specific error code +func NewPluginError(code string, err error) *PluginError { pe := &PluginError{ - code: code, - err: err.Error(), + code: code, + err: err.Error(), } return pe -} \ No newline at end of file +} From 05830cb854ed2008ed3ad37c997901115756f486 Mon Sep 17 00:00:00 2001 From: Drakirus Date: Thu, 22 Aug 2019 15:44:33 +0200 Subject: [PATCH 3/3] makes the MethodChannel Error get vet complient --- plugin/method-channel.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugin/method-channel.go b/plugin/method-channel.go index 70bc2bb8..f28fdd38 100644 --- a/plugin/method-channel.go +++ b/plugin/method-channel.go @@ -213,7 +213,7 @@ func (m *MethodChannel) handleMethodCall(handler MethodHandler, methodName strin var errorCode string switch t := err.(type) { - case *PluginError: + case *Error: errorCode = t.code default: errorCode = "error" @@ -233,22 +233,22 @@ func (m *MethodChannel) handleMethodCall(handler MethodHandler, methodName strin responseSender.Send(binaryReply) } -// This error can be thrown from a go-flutter plugin. Useful if -// you are interested in returning custom error codes. -// If that is not the case, you can just throw normal Go 'error's -type PluginError struct { +// Error implement the Go error interface, can be thrown from a go-flutter +// method channel plugin to return custom error codes. +// Normal Go error can also be used, the error code will default to "error". +type Error struct { err string code string } -// Needed to comply with the Golang 'error' interface -func (e *PluginError) Error() string { +// Error is needed to comply with the Golang error interface. +func (e *Error) Error() string { return e.err } -// Create an error with an specific error code -func NewPluginError(code string, err error) *PluginError { - pe := &PluginError{ +// NewError create an error with an specific error code. +func NewError(code string, err error) *Error { + pe := &Error{ code: code, err: err.Error(), }