mirror of
https://github.com/0xMassi/webclaw.git
synced 2026-06-17 23:55:13 +02:00
Rescued from the stale perf/audit-fixes branch and ported cleanly onto current main. OSS surfaces can now search without the hosted webclaw API when the caller supplies their own Serper.dev key (free at serper.dev). - webclaw-fetch::search() — calls Serper.dev directly (plain wreq client; a JSON API needs no fingerprinting) and, with scrape=true, fetches + extracts the top result pages concurrently (bounded) via the caller's FetchClient. parse_serper_organic() is pure and unit-tested. - MCP `search` tool: local-first — uses SERPER_API_KEY when set, else falls back to the hosted webclaw API. Adds country/lang/scrape params. - OSS REST server: POST /v1/search, gated on SERPER_API_KEY (501 when unset, with a setup hint). Adds ApiError::NotImplemented. - CLI: `webclaw search <query> [--serper-key|SERPER_API_KEY] [--num] [--country] [--lang] [--scrape] [--format]`. No new dependencies (reuses futures-util already in the tree). Original work by the prior author on perf/audit-fixes; this re-applies only the search slice onto main. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
110 lines
3.2 KiB
Rust
110 lines
3.2 KiB
Rust
//! API error type. Maps internal errors to HTTP status codes + JSON.
|
|
|
|
use axum::{
|
|
Json,
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use serde_json::json;
|
|
use thiserror::Error;
|
|
|
|
/// Public-facing API error. Always serializes as `{ "error": "..." }`.
|
|
/// Keep messages user-actionable; internal details belong in tracing logs.
|
|
///
|
|
/// `Unauthorized` / `NotFound` / `Internal` are kept on the enum as
|
|
/// stable variants for handlers that don't exist yet (planned: per-key
|
|
/// rate-limit responses, dynamic route 404s). Marking them dead-code-OK
|
|
/// is preferable to inventing them later in three places.
|
|
#[allow(dead_code)]
|
|
#[derive(Debug, Error)]
|
|
pub enum ApiError {
|
|
#[error("{0}")]
|
|
BadRequest(String),
|
|
|
|
#[error("unauthorized")]
|
|
Unauthorized,
|
|
|
|
#[error("not found")]
|
|
NotFound,
|
|
|
|
#[error("upstream fetch failed: {0}")]
|
|
Fetch(String),
|
|
|
|
#[error("extraction failed: {0}")]
|
|
Extract(String),
|
|
|
|
#[error("LLM provider error: {0}")]
|
|
Llm(String),
|
|
|
|
#[error("internal: {0}")]
|
|
Internal(String),
|
|
|
|
#[error("{0}")]
|
|
NotImplemented(String),
|
|
}
|
|
|
|
impl ApiError {
|
|
pub fn bad_request(msg: impl Into<String>) -> Self {
|
|
Self::BadRequest(msg.into())
|
|
}
|
|
pub fn internal(msg: impl Into<String>) -> Self {
|
|
Self::Internal(msg.into())
|
|
}
|
|
/// 501 — a capability the operator hasn't configured (e.g. search
|
|
/// without `SERPER_API_KEY`). Distinct from `BadRequest` (client's
|
|
/// fault) and `Internal` (our fault): it's a deployment-config gap.
|
|
pub fn not_implemented(msg: impl Into<String>) -> Self {
|
|
Self::NotImplemented(msg.into())
|
|
}
|
|
|
|
fn status(&self) -> StatusCode {
|
|
match self {
|
|
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
|
|
Self::Unauthorized => StatusCode::UNAUTHORIZED,
|
|
Self::NotFound => StatusCode::NOT_FOUND,
|
|
Self::Fetch(_) => StatusCode::BAD_GATEWAY,
|
|
Self::Extract(_) | Self::Llm(_) => StatusCode::UNPROCESSABLE_ENTITY,
|
|
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Self::NotImplemented(_) => StatusCode::NOT_IMPLEMENTED,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IntoResponse for ApiError {
|
|
fn into_response(self) -> Response {
|
|
let body = Json(json!({ "error": self.to_string() }));
|
|
(self.status(), body).into_response()
|
|
}
|
|
}
|
|
|
|
impl From<webclaw_fetch::FetchError> for ApiError {
|
|
fn from(e: webclaw_fetch::FetchError) -> Self {
|
|
match e {
|
|
webclaw_fetch::FetchError::InvalidUrl(msg) => {
|
|
Self::BadRequest(format!("invalid url: {msg}"))
|
|
}
|
|
other => {
|
|
let msg = other.to_string();
|
|
if msg.contains("invalid url:")
|
|
|| msg.contains("blocked private or internal address")
|
|
{
|
|
Self::BadRequest(msg)
|
|
} else {
|
|
Self::Fetch(msg)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<webclaw_core::ExtractError> for ApiError {
|
|
fn from(e: webclaw_core::ExtractError) -> Self {
|
|
Self::Extract(e.to_string())
|
|
}
|
|
}
|
|
|
|
impl From<webclaw_llm::LlmError> for ApiError {
|
|
fn from(e: webclaw_llm::LlmError) -> Self {
|
|
Self::Llm(e.to_string())
|
|
}
|
|
}
|