Files
infix/src/webui/internal/restconf/errors.go
T
Joachim Wiberg 55ecdf3525 webui: configure interfaces, initial wizard support
Idea of wizard is to simplify:

 - Re-adding Ethernet interfaces you've accidentally removed
 - Setting up WiFi access point/station, incl. radio & PSK
 - VLAN filtering bridges

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-06-02 21:11:44 +02:00

86 lines
2.1 KiB
Go

// SPDX-License-Identifier: MIT
package restconf
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
// IsNotFound reports whether err is a RESTCONF error with HTTP status 404.
// Used to distinguish "data resource absent" (expected for delete-or-create
// flows) from real failures.
func IsNotFound(err error) bool {
var e *Error
return errors.As(err, &e) && e.StatusCode == http.StatusNotFound
}
// AuthError is returned when RESTCONF rejects credentials (401/403).
type AuthError struct {
Code int
}
func (e *AuthError) Error() string {
return fmt.Sprintf("authentication failed (HTTP %d)", e.Code)
}
// Error represents a RESTCONF error response.
type Error struct {
StatusCode int
Type string
Tag string
Message string
}
func (e *Error) Error() string {
if e.Message != "" {
return fmt.Sprintf("restconf %d: %s", e.StatusCode, e.Message)
}
return fmt.Sprintf("restconf %d: %s", e.StatusCode, e.Tag)
}
// parseError reads a RESTCONF error response body and returns an *Error.
func parseError(resp *http.Response) error {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 8192))
re := &Error{StatusCode: resp.StatusCode}
// Try to parse the standard RESTCONF error envelope.
var envelope struct {
Errors struct {
Error []struct {
ErrorType string `json:"error-type"`
ErrorTag string `json:"error-tag"`
ErrorPath string `json:"error-path"`
ErrorMessage string `json:"error-message"`
ErrorInfo any `json:"error-info"`
} `json:"error"`
} `json:"ietf-restconf:errors"`
}
if json.Unmarshal(body, &envelope) == nil && len(envelope.Errors.Error) > 0 {
var parts []string
for _, e := range envelope.Errors.Error {
msg := e.ErrorMessage
if msg == "" {
msg = e.ErrorTag
}
if e.ErrorPath != "" {
msg += " (path: " + e.ErrorPath + ")"
}
parts = append(parts, msg)
}
re.Type = envelope.Errors.Error[0].ErrorType
re.Tag = envelope.Errors.Error[0].ErrorTag
re.Message = strings.Join(parts, "; ")
} else {
re.Message = http.StatusText(resp.StatusCode)
}
return re
}