Skip to content

Error Handling and Responses

Arun Prakash edited this page Dec 1, 2025 · 3 revisions

πŸ›‘οΈ Error Handling & Responses

All operations return WordpressResponse<T> which is either:

  • WordpressSuccessResponse<T> with data, code, message, headers, duration
  • WordpressFailureResponse with error, code, headers, duration

Options to handle responses

final resp = await client.posts.list(ListPostRequest(perPage: 10));

// 1) Pattern matching
switch (resp) {
  case WordpressSuccessResponse():
    print(resp.data);
  case WordpressFailureResponse():
    print(resp.error);
}

// 2) map
final items = resp.map(
  onSuccess: (s) => s.data,
  onFailure: (_) => <Post>[],
);

// 3) Throw on failure
final posts = (await client.posts.list(ListPostRequest(perPage: 10))).dataOrThrow();

// 4) Access timings/headers
final ms = resp.duration.inMilliseconds;
final code = resp.code;

Raw responses

If you need untyped responses for storage or custom parsing, use the raw APIs.

final raw = await client.raw(
  WordpressRequest(
    method: HttpMethod.get,
    url: RequestUrl.relative('posts'),
  ),
);
print(raw.code);
print(raw.data); // bytes / string / json

Tip: Combine with the Caching middleware for efficient repeated GETs.

Clone this wiki locally