Compare commits
1
Commits
master
...
better-errors
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
722e0ad3e3 |
+21
-81
@@ -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 {
|
|
||||||
return applicationDoesNotExistsError{
|
|
||||||
baseWebsocketError{Code: 4001, Msg: "Could not found an app with the given key"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The user did not send the protocol version
|
// The user did not send the protocol version
|
||||||
type noProtocolVersionSuppliedError struct {
|
noProtocolVersionSuppliedError = newWebsocketError(4008, "No protocol version supplied")
|
||||||
baseWebsocketError
|
|
||||||
}
|
|
||||||
|
|
||||||
func newNoProtocolVersionSuppliedError() noProtocolVersionSuppliedError {
|
|
||||||
return noProtocolVersionSuppliedError{
|
|
||||||
baseWebsocketError{Code: 4008, Msg: "No protocol version supplied"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// When the application is disabled.
|
// When the application is disabled.
|
||||||
// See the configuration file
|
// See the configuration file
|
||||||
type applicationDisabledError struct {
|
applicationDisabledError = newWebsocketError(4003, "Application disabled")
|
||||||
baseWebsocketError
|
|
||||||
}
|
|
||||||
|
|
||||||
func newApplicationDisabledError() noProtocolVersionSuppliedError {
|
|
||||||
return noProtocolVersionSuppliedError{
|
|
||||||
baseWebsocketError{Code: 4003, Msg: "Application disabled"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// When the application only accepts SSL connections
|
// When the application only accepts SSL connections
|
||||||
type applicationOnlyAccepsSSLError struct {
|
applicationOnlyAccepsSSLError = newWebsocketError(4000, "Application only accepts SSL connections, reconnect using wss://")
|
||||||
baseWebsocketError
|
|
||||||
}
|
|
||||||
|
|
||||||
func newApplicationOnlyAccepsSSLError() applicationOnlyAccepsSSLError {
|
|
||||||
return applicationOnlyAccepsSSLError{
|
|
||||||
baseWebsocketError{Code: 4000, Msg: "Application only accepts SSL connections, reconnect using wss://"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// When the user send an invalid version
|
// When the user send an invalid version
|
||||||
type invalidVersionStringFormatError struct {
|
invalidVersionStringFormatError = newWebsocketError(4006, "Invalid version string format")
|
||||||
baseWebsocketError
|
|
||||||
}
|
|
||||||
|
|
||||||
func newInvalidVersionStringFormatError() invalidVersionStringFormatError {
|
|
||||||
return invalidVersionStringFormatError{
|
|
||||||
baseWebsocketError{Code: 4006, Msg: "Invalid version string format"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used when the error was internal
|
// Used when the error was internal
|
||||||
// * Decoding json
|
// * Decoding json
|
||||||
// * Writing to output
|
// * Writing to output
|
||||||
type genericReconnectImmediatelyError struct {
|
genericReconnectImmediatelyError = newWebsocketError(4200, "Generic reconnect immediately")
|
||||||
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
|
// 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
|
// Currently I do not know how to send nil, so I send GENERIC_ERROR
|
||||||
type genericError struct {
|
genericError = newWebsocketError(0, "Generic Error")
|
||||||
baseWebsocketError
|
|
||||||
}
|
|
||||||
|
|
||||||
func newGenericError(msg string) genericError {
|
disabledClientEventsError = websocketError{Msg: "To send client events, you must enable this feature in the Settings."}
|
||||||
return genericError{
|
|
||||||
baseWebsocketError{Code: 0, Msg: msg},
|
couldNotFoundChannelError = websocketError{Msg: "Could not find a channel with the given id"}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|||||||
+4
-11
@@ -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
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user