$methods, 'callback' => function (\WP_REST_Request $request) use ($callback) { try { $result = $callback($request); return $result instanceof \WP_REST_Response ? $result : rest_ensure_response($result); } catch (RelayError $e) { return new \WP_Error($e->error_code(), $e->text(), [ 'status' => self::status_for($e), 'hint' => $e->hint(), 'retryable' => $e->retryable(), 'context' => $e->context(), ]); } }, 'permission_callback' => [Capability::class, 'check'], ]); } public static function status_for(RelayError $e): int { $code = $e->error_code(); if (in_array($code, ['job.not_found', 'backup.not_found', 'keys.not_found', 'connection.not_found', 'import.not_found'], true)) { return 404; } if (in_array($code, ['job.busy', 'job.active_exists', 'upload.offset_mismatch', 'backup.in_use', 'job.receiver_driven'], true)) { return 409; } if (strpos($code, 'db.') === 0 || $code === 'job.unexpected') { return 500; } return $e->retryable() ? 503 : 400; } }