From 234b0a2e068d3f2651e38e7442cbe46752b71c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 16 Nov 2020 16:56:03 +0100 Subject: [PATCH] remove code duplication. --- internal/websocket/handler/handler.go | 98 ++++++++++++--------------- 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/internal/websocket/handler/handler.go b/internal/websocket/handler/handler.go index 6e34b8df..34f28773 100644 --- a/internal/websocket/handler/handler.go +++ b/internal/websocket/handler/handler.go @@ -77,101 +77,93 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error { return err } + var err error switch header.Event { // Signal Events case event.SIGNAL_ANSWER: payload := &message.SignalAnswer{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.signalAnswer(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.signalAnswer(session, payload) + }) // Control Events case event.CONTROL_RELEASE: - return errors.Wrapf(h.controlRelease(session), "%s failed", header.Event) + err = h.controlRelease(session) case event.CONTROL_REQUEST: - return errors.Wrapf(h.controlRequest(session), "%s failed", header.Event) + err = h.controlRequest(session) case event.CONTROL_GIVE: payload := &message.Control{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.controlGive(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.controlGive(session, payload) + }) case event.CONTROL_CLIPBOARD: payload := &message.Clipboard{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.controlClipboard(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.controlClipboard(session, payload) + }) case event.CONTROL_KEYBOARD: payload := &message.Keyboard{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.controlKeyboard(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.controlKeyboard(session, payload) + }) // Screen Events case event.SCREEN_RESOLUTION: - return errors.Wrapf(h.screenResolution(session), "%s failed", header.Event) + err = h.screenResolution(session) case event.SCREEN_CONFIGURATIONS: - return errors.Wrapf(h.screenConfigurations(session), "%s failed", header.Event) + err = h.screenConfigurations(session) case event.SCREEN_SET: payload := &message.ScreenResolution{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.screenSet(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.screenSet(session, payload) + }) // Boradcast Events case event.BORADCAST_CREATE: payload := &message.BroadcastCreate{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.boradcastCreate(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.boradcastCreate(session, payload) + }) case event.BORADCAST_DESTROY: - return errors.Wrapf(h.boradcastDestroy(session), "%s failed", header.Event) + err = h.boradcastDestroy(session) // Admin Events case event.ADMIN_LOCK: - return errors.Wrapf(h.adminLock(session), "%s failed", header.Event) + err = h.adminLock(session) case event.ADMIN_UNLOCK: - return errors.Wrapf(h.adminUnlock(session), "%s failed", header.Event) + err = h.adminUnlock(session) case event.ADMIN_CONTROL: - return errors.Wrapf(h.adminControl(session), "%s failed", header.Event) + err = h.adminControl(session) case event.ADMIN_RELEASE: - return errors.Wrapf(h.adminRelease(session), "%s failed", header.Event) + err = h.adminRelease(session) case event.ADMIN_GIVE: payload := &message.Admin{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.adminGive(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.adminGive(session, payload) + }) case event.ADMIN_BAN: payload := &message.Admin{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.adminBan(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.adminBan(session, payload) + }) case event.ADMIN_KICK: payload := &message.Admin{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.adminKick(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.adminKick(session, payload) + }) case event.ADMIN_MUTE: payload := &message.Admin{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.adminMute(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.adminMute(session, payload) + }) case event.ADMIN_UNMUTE: payload := &message.Admin{} - return errors.Wrapf( - utils.Unmarshal(payload, raw, func() error { - return h.adminUnmute(session, payload) - }), "%s failed", header.Event) + err = utils.Unmarshal(payload, raw, func() error { + return h.adminUnmute(session, payload) + }) default: return errors.Errorf("unknown message event %s", header.Event) } + + return errors.Wrapf(err, "%s failed", header.Event) }