Debian Codegraph @

pkg/services/push/mqtt/mqtt_errors.go

raw
1 package mqtt
2
3 import (
4 "errors"
5 "fmt"
6 )
7
8 // PublishError represents an MQTT v5 publish failure with a reason code.
9 // MQTT v5 reason codes >= 0x80 indicate failures, while codes < 0x80 indicate
10 // success or non-fatal conditions that should be logged but not treated as errors.
11 type PublishError struct {
12 // ReasonCode is the MQTT v5 reason code returned by the broker.
13 ReasonCode byte
14 // ReasonString is the optional human-readable reason description from the broker.
15 ReasonString string
16 }
17
18 // reasonCodeFailureThreshold is the threshold at which MQTT v5 reason codes
19 // indicate failure. Codes >= 0x80 are failure/error codes per the MQTT v5 specification.
20 // Codes 0x00-0x7F are success or non-fatal warning codes.
21 const reasonCodeFailureThreshold = 0x80
22
23 // Error definitions for the MQTT service.
24 var (
25 // ErrPublishTimeout is returned when message publication exceeds the configured timeout.
26 // This error indicates the broker did not acknowledge the message within the expected time.
27 ErrPublishTimeout = errors.New("publish timeout exceeded")
28
29 // ErrConnectionNotInitialized is returned when the connection manager fails to initialize.
30 ErrConnectionNotInitialized = errors.New("MQTT connection manager not initialized")
31
32 // ErrTopicRequired is returned when a configuration URL lacks a required topic path.
33 // The topic is mandatory for publishing messages and must be provided in the URL path.
34 ErrTopicRequired = errors.New("topic is required")
35
36 // ErrInvalidQoS is returned when a QoS value is outside the valid range (0-2).
37 // MQTT protocol only supports QoS levels 0, 1, and 2.
38 ErrInvalidQoS = errors.New("invalid QoS value: must be 0, 1, or 2")
39
40 // ErrNoDialContext is returned when a custom connection attempt runs without a dialer.
41 ErrNoDialContext = errors.New("custom dial context is not configured")
42
43 // ErrPasswordWithoutUsername is returned when a password is provided without a username.
44 // Password credentials require a username to be included in the URL.
45 ErrPasswordWithoutUsername = errors.New(
46 "password provided without username: username is required when using password authentication",
47 )
48 )
49
50 // Error implements the error interface for PublishError.
51 // It returns a formatted error message including the reason code in hex format
52 // and the optional reason string if provided.
53 func (e PublishError) Error() string {
54 // Build the base error message with hex-formatted reason code
55 msg := fmt.Sprintf("MQTT publish failed: reason code 0x%02X", e.ReasonCode)
56
57 // Append the reason string if provided by the broker
58 if e.ReasonString != "" {
59 msg = fmt.Sprintf("%s - %s", msg, e.ReasonString)
60 }
61
62 return msg
63 }
64
65 // IsFailureCode checks if an MQTT v5 reason code indicates a failure.
66 // Reason codes >= 0x80 are failure codes per the MQTT v5 specification.
67 //
68 // Returns true if the code indicates a failure that should be returned as an error,
69 // false if the code indicates success or a non-fatal condition.
70 func IsFailureCode(code byte) bool {
71 return code >= reasonCodeFailureThreshold
72 }