Skip to content

Commit

Permalink
emulation clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Dec 14, 2024
1 parent 756ee3d commit e83dc78
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions emulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *EmulationHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
err = s.emuLayer.Emulate(&req)
if err != nil {
s.node.logger.log(newLogEntry(LogLevelError, "error processing emulation request", map[string]any{"req": &req, "error": err.Error()}))
if err == errNodeNotFound {
if errors.Is(err, errNodeNotFound) {
rw.WriteHeader(http.StatusNotFound)
} else {
rw.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -126,17 +126,22 @@ func newEmulationSurveyHandler(node *Node) *emulationSurveyHandler {
return &emulationSurveyHandler{node: node}
}

const (
emulationErrorCodeBadRequest = 1
emulationErrorCodeNoSession = 2
)

func (h *emulationSurveyHandler) HandleEmulation(e SurveyEvent, cb SurveyCallback) {
var req protocol.EmulationRequest
err := req.UnmarshalVT(e.Data)
if err != nil {
h.node.logger.log(newLogEntry(LogLevelError, "error unmarshal emulation request", map[string]any{"data": string(e.Data), "error": err.Error()}))
cb(SurveyReply{Code: 1})
cb(SurveyReply{Code: emulationErrorCodeBadRequest})
return
}
client, ok := h.node.Hub().clientBySession(req.Session)
if !ok {
cb(SurveyReply{Code: 2})
cb(SurveyReply{Code: emulationErrorCodeNoSession})
return
}
var data []byte
Expand All @@ -145,7 +150,7 @@ func (h *emulationSurveyHandler) HandleEmulation(e SurveyEvent, cb SurveyCallbac
err = json.Unmarshal(req.Data, &d)
if err != nil {
h.node.logger.log(newLogEntry(LogLevelError, "error unmarshal emulation request data", map[string]any{"data": string(req.Data), "error": err.Error()}))
cb(SurveyReply{Code: 3})
cb(SurveyReply{Code: emulationErrorCodeBadRequest})
return
}
data = []byte(d)
Expand Down

0 comments on commit e83dc78

Please sign in to comment.