-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
130 lines (112 loc) · 3.5 KB
/
errors.go
File metadata and controls
130 lines (112 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2025 Matthew Gall <me@matthewgall.dev>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net/http"
)
// APIError represents an error from the Octopus Energy API
type APIError struct {
StatusCode int
Endpoint string
Message string
Retryable bool
Err error // Underlying error if any
}
func (e *APIError) Error() string {
if e.Err != nil {
return fmt.Sprintf("API error (%d) at %s: %s (caused by: %v)", e.StatusCode, e.Endpoint, e.Message, e.Err)
}
return fmt.Sprintf("API error (%d) at %s: %s", e.StatusCode, e.Endpoint, e.Message)
}
func (e *APIError) Unwrap() error {
return e.Err
}
// NewAPIError creates a new APIError with automatic retryable detection
func NewAPIError(statusCode int, endpoint, message string, err error) *APIError {
return &APIError{
StatusCode: statusCode,
Endpoint: endpoint,
Message: message,
Retryable: isRetryableStatus(statusCode),
Err: err,
}
}
// isRetryableStatus determines if an HTTP status code is retryable
func isRetryableStatus(statusCode int) bool {
switch statusCode {
case http.StatusTooManyRequests, // 429
http.StatusInternalServerError, // 500
http.StatusBadGateway, // 502
http.StatusServiceUnavailable, // 503
http.StatusGatewayTimeout: // 504
return true
default:
return false
}
}
// AuthError represents authentication/authorization errors
type AuthError struct {
Code string // Error code from API (e.g., "KT-CT-1139")
Message string
Err error
}
func (e *AuthError) Error() string {
if e.Code != "" {
return fmt.Sprintf("authentication error [%s]: %s", e.Code, e.Message)
}
return fmt.Sprintf("authentication error: %s", e.Message)
}
func (e *AuthError) Unwrap() error {
return e.Err
}
// CacheError represents errors related to cache operations
type CacheError struct {
CacheType string // e.g., "saving_sessions", "meter_devices"
Operation string // e.g., "read", "write", "validate"
Err error
}
func (e *CacheError) Error() string {
return fmt.Sprintf("cache error for %s during %s: %v", e.CacheType, e.Operation, e.Err)
}
func (e *CacheError) Unwrap() error {
return e.Err
}
// ValidationError represents configuration or input validation errors
type ValidationError struct {
Field string
Value interface{}
Message string
}
func (e *ValidationError) Error() string {
if e.Value != nil {
return fmt.Sprintf("validation error for %s (value: %v): %s", e.Field, e.Value, e.Message)
}
return fmt.Sprintf("validation error for %s: %s", e.Field, e.Message)
}
// SessionError represents errors specific to saving session operations
type SessionError struct {
SessionID string
Operation string // e.g., "join", "leave", "fetch"
Err error
}
func (e *SessionError) Error() string {
if e.SessionID != "" {
return fmt.Sprintf("session error [%s] during %s: %v", e.SessionID, e.Operation, e.Err)
}
return fmt.Sprintf("session error during %s: %v", e.Operation, e.Err)
}
func (e *SessionError) Unwrap() error {
return e.Err
}