BareGit
#pragma once

#include <optional>
#include <string>
#include <string_view>

#include <mw/error.hpp>

namespace telegrammer
{

/// Describes an expected failure and its local HTTP interpretation.
struct ServiceError
{
    /// HTTP status used when this error crosses the API boundary.
    int status = 500;
    /// Stable machine-readable error code.
    std::string code;
    /// Sanitized human-readable explanation.
    std::string msg;
    /// Optional client retry delay in seconds.
    std::optional<int> retry_after;
    /// Whether background workers may retry the operation.
    bool retryable = false;
};

/// Construct a typed service error for an expected failure.
inline mw::Error serviceError(int status, std::string_view code,
                              std::string_view message,
                              std::optional<int> retry_after = std::nullopt,
                              bool retryable = false)
{
    return ServiceError{status, std::string(code), std::string(message),
                        retry_after, retryable};
}

/// Return the typed service error stored in a libmw error, if any.
inline const ServiceError* asServiceError(const mw::Error& error)
{
    return error.as<ServiceError>();
}

} // namespace telegrammer