Record
public record PsemError : System.ValueType, System.IEquatable<SharpMeter.Core.Protocol.PsemError>

Namespace: SharpMeter.Core.Protocol

Represents an error that occurred during PSEM communication.

Inheritance

Inherits from: System.ValueType

Implements: System.IEquatable<SharpMeter.Core.Protocol.PsemError>

Constructors

NameDescription
PsemError(…) Represents an error that occurred during PSEM communication.

PsemError(PsemErrorCode Code, string Message, ResponseCode? ResponseCode)

PsemError.PsemError(PsemErrorCode Code, string Message, ResponseCode? ResponseCode = null)

Represents an error that occurred during PSEM communication.

Parameters

NameTypeDescription
CodeSharpMeter.Core.Protocol.PsemErrorCodeThe error code from the protocol or transport layer.
MessagestringA human-readable error description.
ResponseCodeSharpMeter.Core.Protocol.ResponseCode?The optional L7 response code from the meter.

Properties

NameDescription
Code The error code from the protocol or transport layer.
Message A human-readable error description.
ResponseCode The optional L7 response code from the meter.

Code

PsemErrorCode PsemError.Code { get; init; }

The error code from the protocol or transport layer.

Message

string PsemError.Message { get; init; }

A human-readable error description.

ResponseCode

ResponseCode? PsemError.ResponseCode { get; init; }

The optional L7 response code from the meter.

Methods

NameDescription
CrcMismatch(ushort expected, ushort actual) static Creates a CRC mismatch error.
Framing(string message) static Creates a framing error.
InvalidState(string message) static Creates an invalid state error.
Protocol(ResponseCode code) static Creates a protocol-level error from a meter response.
SecurityFailure(string message) static Creates a security/authentication error.
Timeout(string operation) static Creates a timeout error.
ToString() override
Transport(string message) static Creates a transport-level error.

CrcMismatch(ushort expected, ushort actual)

PsemError PsemError.CrcMismatch(ushort expected, ushort actual)

Creates a CRC mismatch error.

Framing(string message)

PsemError PsemError.Framing(string message)

Creates a framing error.

InvalidState(string message)

PsemError PsemError.InvalidState(string message)

Creates an invalid state error.

Protocol(ResponseCode code)

PsemError PsemError.Protocol(ResponseCode code)

Creates a protocol-level error from a meter response.

SecurityFailure(string message)

PsemError PsemError.SecurityFailure(string message)

Creates a security/authentication error.

Timeout(string operation)

PsemError PsemError.Timeout(string operation)

Creates a timeout error.

Transport(string message)

PsemError PsemError.Transport(string message)

Creates a transport-level error.

Type Relationships
classDiagram
                    style PsemError fill:#f9f,stroke:#333,stroke-width:2px
                    PsemError --|> ValueType : inherits
                    PsemError ..|> PsemError~ : implements
                
View Source
/// <summary>
///     Represents an error that occurred during PSEM communication.
/// </summary>
/// <param name = "Code">The error code from the protocol or transport layer.</param>
/// <param name = "Message">A human-readable error description.</param>
/// <param name = "ResponseCode">The optional L7 response code from the meter.</param>
public readonly record struct PsemError(PsemErrorCode Code, string Message, ResponseCode? ResponseCode = null)
{
#region Overrides
    /// <inheritdoc/>
    public override string ToString() => ResponseCode.HasValue ? $"[{Code}] {Message} (Response: {ResponseCode.Value})" : $"[{Code}] {Message}";
#endregion
#region Factory Methods
    /// <summary>Creates a transport-level error.</summary>
    public static PsemError Transport(string message) => new(PsemErrorCode.TransportError, message);
    /// <summary>Creates a CRC mismatch error.</summary>
    public static PsemError CrcMismatch(ushort expected, ushort actual) => new(PsemErrorCode.CrcMismatch, $"CRC mismatch: expected 0x{expected:X4}, got 0x{actual:X4}");
    /// <summary>Creates a timeout error.</summary>
    public static PsemError Timeout(string operation) => new(PsemErrorCode.Timeout, $"Timeout during {operation}");
    /// <summary>Creates a protocol-level error from a meter response.</summary>
    public static PsemError Protocol(ResponseCode code) => new(PsemErrorCode.ProtocolError, $"Meter returned {code}", code);
    /// <summary>Creates a framing error.</summary>
    public static PsemError Framing(string message) => new(PsemErrorCode.FramingError, message);
    /// <summary>Creates an invalid state error.</summary>
    public static PsemError InvalidState(string message) => new(PsemErrorCode.InvalidState, message);
    /// <summary>Creates a security/authentication error.</summary>
    public static PsemError SecurityFailure(string message) => new(PsemErrorCode.SecurityFailure, message, Core.Protocol.ResponseCode.InsufficientSecurityClearance);
#endregion
}
Was this page helpful?