OKHAM.org Documentación EnglishCastellano

Edición publicada en febrero de 2026

Documento original en inglés

Error Handling Specification

Disclaimer: This document is non-normative and describes recommended practices for the OKHAM ecosystem.

Version 1.0.0

Status: DRAFT

This document specifies the error handling patterns for all OKHAM capabilities.


Golden Rules

1. Never Swallow Errors

FORBIDDEN:
- if err != nil { return }  // Silent failure
- if err != nil { log.Print(err) }  // Log only, no propagation
- _ = someFunction()  // Ignoring returned error

Required Pattern:

if err != nil {
    return fmt.Errorf("context for what failed: %w", err)
}

2. Always Wrap Errors with Context

// WRONG
if err != nil {
    return err
}

// CORRECT
if err != nil {
    return fmt.Errorf("failed to load camera %s: %w", cameraID, err)
}

3. Use Structured Logging for Errors

// WRONG - unstructured
log.Printf("error: %v", err)

// CORRECT - structured with context
rt.Logger.Error("failed to process message",
    rt.F.Str("subject", msg.Subject),
    rt.F.Err(err),
)

HTTP Error Responses

All HTTP handlers MUST return consistent error responses:

// Standard error response structure
type ErrorResponse struct {
    Error   string `json:"error"`
    Code    string `json:"code,omitempty"`
    Details string `json:"details,omitempty"`
}

// Usage
http.Error(w, `{"error":"camera not found","code":"NOT_FOUND"}`, http.StatusNotFound)

HTTP Status Code Usage

Scenario Status Code
Resource not found 404
Invalid request body/params 400
Unauthorized 401
Forbidden 403
Internal error 500
Service unavailable 503

Handler Pattern

func handleGetResource(w http.ResponseWriter, r *http.Request) {
    id := r.PathValue("id")

    resource, err := s.store.Get(id)
    if err != nil {
        if err == ErrNotFound {
            rt.Logger.Warn("resource not found", rt.F.Str("id", id))
            http.Error(w, `{"error":"resource not found","code":"NOT_FOUND"}`, http.StatusNotFound)
            return
        }
        rt.Logger.Error("failed to get resource", rt.F.Str("id", id), rt.F.Err(err))
        http.Error(w, `{"error":"internal error"}`, http.StatusInternalServerError)
        return
    }

    // Success response...
}

NATS Message Error Handling

For Request/Reply

// Handler MUST respond with error payload, not just log
func handleRequest(msg *nats.Msg, rt *capabilityruntime.RuntimeContext) {
    result, err := process(msg.Data)
    if err != nil {
        rt.Log.Error("processing failed", rt.F.Err(err))
        msg.Respond([]byte(`{"error":"` + err.Error() + `"}`))
        return
    }
    msg.Respond(result)
}

For Stream Consumers

// Use NAK for retryable errors, TERM for permanent failures
if isRetryable(err) {
    msg.Nak()
} else {
    rt.Log.Error("permanent failure, terminating message", rt.F.Err(err))
    msg.Term()
}

Panic Recovery

All entry points MUST have panic recovery:

// REQUIRED in HTTP handlers
defer func() {
    if r := recover(); r != nil {
        rt.Log.Error("panic recovered", rt.F.Any("panic", r))
        http.Error(w, `{"error":"internal server error"}`, http.StatusInternalServerError)
    }
}()