Skip to content

Commit 546ed2a

Browse files
committed
fix: error states
1 parent 885a1ed commit 546ed2a

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

internal/api/station_handler.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,19 @@ func (h *StationHandler) StartCharging(w http.ResponseWriter, r *http.Request) {
652652

653653
// Start charging
654654
if err := h.manager.StartCharging(r.Context(), stationID, req.ConnectorID, req.IDTag); err != nil {
655-
h.sendError(w, http.StatusInternalServerError, err.Error())
655+
errMsg := err.Error()
656+
// Determine appropriate status code based on error type
657+
statusCode := http.StatusInternalServerError
658+
if strings.Contains(errMsg, "not found") {
659+
statusCode = http.StatusNotFound
660+
} else if strings.Contains(errMsg, "not available") || strings.Contains(errMsg, "reserved") {
661+
statusCode = http.StatusConflict
662+
} else if strings.Contains(errMsg, "not connected") {
663+
statusCode = http.StatusServiceUnavailable
664+
} else if strings.Contains(errMsg, "authorization") || strings.Contains(errMsg, "rejected") {
665+
statusCode = http.StatusForbidden
666+
}
667+
h.sendError(w, statusCode, errMsg)
656668
return
657669
}
658670

@@ -704,7 +716,17 @@ func (h *StationHandler) StopCharging(w http.ResponseWriter, r *http.Request) {
704716

705717
// Stop charging
706718
if err := h.manager.StopCharging(r.Context(), stationID, req.ConnectorID, req.Reason); err != nil {
707-
h.sendError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to stop charging: %v", err))
719+
errMsg := err.Error()
720+
// Determine appropriate status code based on error type
721+
statusCode := http.StatusInternalServerError
722+
if strings.Contains(errMsg, "not found") {
723+
statusCode = http.StatusNotFound
724+
} else if strings.Contains(errMsg, "no active") || strings.Contains(errMsg, "not charging") {
725+
statusCode = http.StatusConflict
726+
} else if strings.Contains(errMsg, "not connected") {
727+
statusCode = http.StatusServiceUnavailable
728+
}
729+
h.sendError(w, statusCode, fmt.Sprintf("Failed to stop charging: %v", err))
708730
return
709731
}
710732

0 commit comments

Comments
 (0)