Skip to content
Repos

DevPlaceApp

C++ 904 B26 lines Opens in site

Directory Raw Download

#pragma once
// Spec: models.md "API error JSON shape and message selection".
//   { "error": <string | {status?,message?,errors?}>, "messages": [string]? }
// object error: detail.message ?? detail.errors.first ?? messages.first ; string error: messages.first ?? text.
// Specific message -> isSpecific=true, else the generic text with isSpecific=false.
// A non-2xx body that does not decode gives Kind::Http (generic text); 401 gives Kind::Unauthorized.
#include <optional>
#include <string>

#include "core/Result.hpp"

namespace dp {

class ApiErrorDecoder {
 public:
  struct Decoded {
    std::string message;
    bool isSpecific = false;
  };

  // nullopt when the body is not a usable API error (no `error` key, wrong types, not JSON).
  static std::optional<Decoded> decodeBody(const std::string& body);
  static Error fromResponse(int status, const std::string& body);
};

}  // namespace dp
35.8ms