1 Commits
Author SHA1 Message Date
claudemiro 722e0ad3e3 WIP: Cleaning the error handling... 2016-10-02 23:01:12 -03:00
3 changed files with 37 additions and 104 deletions
+32 -92
View File
@@ -6,119 +6,59 @@ package ipe
import "fmt" import "fmt"
// Base interface
type websocketError interface {
GetCode() int
GetMsg() string
}
// Base struct // Base struct
type baseWebsocketError struct { type websocketError struct {
Code int Code *int
Msg string Msg string
} }
func (e baseWebsocketError) GetCode() int { func (e websocketError) GetCode() *int {
return e.Code return e.Code
} }
func (e baseWebsocketError) GetMsg() string { func (e websocketError) GetMsg() string {
return e.Msg return e.Msg
} }
func (e baseWebsocketError) Error() string { func (e websocketError) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Msg) return fmt.Sprintf("%d: %s", e.Code, e.Msg)
} }
// Unsupprted protocol version func newWebsocketError(code int, msg string) websocketError {
type unsupportedProtocolVersionError struct { return websocketError{Code: &code, Msg: msg}
baseWebsocketError
} }
func newUnsupportedProtocolVersionError() unsupportedProtocolVersionError { var (
return unsupportedProtocolVersionError{ // Unsupprted protocol version
baseWebsocketError{Code: 4007, Msg: "Unsupported protocol version"}, unsupportedProtocolVersionError = newWebsocketError(4007, "Unsupported protocol version")
}
}
// The application does not exists // The application does not exists
// See the configuration file // See the configuration file
type applicationDoesNotExistsError struct { applicationDoesNotExistsError = newWebsocketError(4001, "Could not found an app with the given key")
baseWebsocketError
}
func newApplicationDoesNotExistsError() applicationDoesNotExistsError { // The user did not send the protocol version
return applicationDoesNotExistsError{ noProtocolVersionSuppliedError = newWebsocketError(4008, "No protocol version supplied")
baseWebsocketError{Code: 4001, Msg: "Could not found an app with the given key"},
}
}
// The user did not send the protocol version // When the application is disabled.
type noProtocolVersionSuppliedError struct { // See the configuration file
baseWebsocketError applicationDisabledError = newWebsocketError(4003, "Application disabled")
}
func newNoProtocolVersionSuppliedError() noProtocolVersionSuppliedError { // When the application only accepts SSL connections
return noProtocolVersionSuppliedError{ applicationOnlyAccepsSSLError = newWebsocketError(4000, "Application only accepts SSL connections, reconnect using wss://")
baseWebsocketError{Code: 4008, Msg: "No protocol version supplied"},
}
}
// When the application is disabled. // When the user send an invalid version
// See the configuration file invalidVersionStringFormatError = newWebsocketError(4006, "Invalid version string format")
type applicationDisabledError struct {
baseWebsocketError
}
func newApplicationDisabledError() noProtocolVersionSuppliedError { // Used when the error was internal
return noProtocolVersionSuppliedError{ // * Decoding json
baseWebsocketError{Code: 4003, Msg: "Application disabled"}, // * Writing to output
} genericReconnectImmediatelyError = newWebsocketError(4200, "Generic reconnect immediately")
}
// When the application only accepts SSL connections // When pusher wants to send an Generic error, it only send the message, the code become nil
type applicationOnlyAccepsSSLError struct { // Currently I do not know how to send nil, so I send GENERIC_ERROR
baseWebsocketError genericError = newWebsocketError(0, "Generic Error")
}
func newApplicationOnlyAccepsSSLError() applicationOnlyAccepsSSLError { disabledClientEventsError = websocketError{Msg: "To send client events, you must enable this feature in the Settings."}
return applicationOnlyAccepsSSLError{
baseWebsocketError{Code: 4000, Msg: "Application only accepts SSL connections, reconnect using wss://"},
}
}
// When the user send an invalid version couldNotFoundChannelError = websocketError{Msg: "Could not find a channel with the given id"}
type invalidVersionStringFormatError struct { )
baseWebsocketError
}
func newInvalidVersionStringFormatError() invalidVersionStringFormatError {
return invalidVersionStringFormatError{
baseWebsocketError{Code: 4006, Msg: "Invalid version string format"},
}
}
// Used when the error was internal
// * Decoding json
// * Writing to output
type genericReconnectImmediatelyError struct {
baseWebsocketError
}
func newGenericReconnectImmediatelyError() genericReconnectImmediatelyError {
return genericReconnectImmediatelyError{
baseWebsocketError{Code: 4200, Msg: "Generic reconnect immediately"},
}
}
// When pusher wants to send an Generic error, it only send the message, the code become nil
// Currently I do not know how to send nil, so I send GENERIC_ERROR
type genericError struct {
baseWebsocketError
}
func newGenericError(msg string) genericError {
return genericError{
baseWebsocketError{Code: 0, Msg: msg},
}
}
+4 -11
View File
@@ -160,24 +160,17 @@ type errorEvent struct {
// Create a new error event // Create a new error event
// Pusher protocol is very strange in some parts // Pusher protocol is very strange in some parts
// It send null in some errors. // It send null in some errors.
func newErrorEvent(code int, message string) errorEvent { func newErrorEvent(code *int, message string) errorEvent {
type dataErrorEvent struct { type dataErrorEvent struct {
Code *int `json:"code"` Code *int `json:"code"`
Message string `json:"message"` Message string `json:"message"`
} }
var data = dataErrorEvent{ return errorEvent{Event: "pusher:error", Data: dataErrorEvent{
Code: code,
Message: message, Message: message,
} }}
if code == 0 {
data.Code = nil
} else {
data.Code = &code
}
return errorEvent{Event: "pusher:error", Data: data}
} }
// { // {
+1 -1
View File
@@ -87,7 +87,7 @@ func onOpen(conn *websocket.Conn, r *http.Request, sessionID string, app *app) e
case strings.TrimSpace(p) == "": case strings.TrimSpace(p) == "":
return newNoProtocolVersionSuppliedError() return newNoProtocolVersionSuppliedError()
case protocol != supportedProtocolVersion: case protocol != supportedProtocolVersion:
return newUnsupportedProtocolVersionError() return unsupportedProtocolVersionError
case app.ApplicationDisabled: case app.ApplicationDisabled:
return newApplicationDisabledError() return newApplicationDisabledError()
case app.OnlySSL: case app.OnlySSL: