{"record":{"id":"af5b02f8b0fef830","repo":"mastra-ai/mastra","slug":"bad-request-responsetext","errorCode":null,"errorMessage":"bad request: ${responseText}","messagePattern":"bad request: (.+?)","errorType":"http","errorClass":null,"httpStatus":400,"severity":"error","filePath":"integrations/brightdata/src/client.ts","lineNumber":76,"sourceCode":"    const response = await fetch(REQUEST_ENDPOINT, {\n      body: JSON.stringify(body),\n      headers: {\n        Authorization: `Bearer ${apiKey}`,\n        'Content-Type': 'application/json',\n      },\n      method: 'POST',\n      signal: controller.signal,\n    });\n\n    const responseText = await response.text();\n\n    if (!response.ok) {\n      if (response.status === 401 || response.status === 403) {\n        throw new Error('invalid API key or insufficient permissions');\n      }\n\n      if (response.status === 400) {\n        throw new Error(`bad request: ${responseText}`);\n      }\n\n      throw new Error(`request failed with status ${response.status}: ${responseText}`);\n    }\n\n    if (body.format === 'json') {\n      return responseText ? JSON.parse(responseText) : {};\n    }\n\n    return responseText;\n  } catch (error) {\n    if (error instanceof Error && error.name === 'AbortError') {\n      throw new Error(`Request timed out after ${effectiveTimeout}ms`);\n    }\n    throw error;\n  } finally {\n    clearTimeout(timeoutId);\n  }","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/integrations/brightdata/src/client.ts#L58-L94","documentation":"requestBrightData throws this when Bright Data responds with HTTP 400, indicating the request payload was malformed or contained invalid parameters. The library includes the raw response body (responseText) in the message so the caller can see Bright Data's own complaint. This is a client-side request construction problem, not an auth or availability issue.","triggerScenarios":"Any requestBrightData call where Bright Data returns status 400 — e.g. empty or malformed query passed to client.search.google, invalid option values (bad country/language/zone params) that produce an invalid request URL or body.","commonSituations":"Passing an empty string as the search query; unsupported search parameters forwarded into the URL; a zone name with invalid characters; user-supplied input interpolated into the request without sanitization.","solutions":["Read the responseText appended to the error — it contains Bright Data's specific reason for rejecting the request.","Log and inspect the exact query and options passed to the search call; fix invalid values (empty query, bad params).","Validate/normalize inputs (non-empty query, ISO language/country codes) before calling the client.","Compare the generated request against the Bright Data API docs for the zone being used."],"exampleFix":"// before\nawait client.search.google(userInput); // userInput may be ''\n// after\nconst q = userInput.trim();\nif (!q) throw new Error('search query must be non-empty');\nawait client.search.google(q);","handlingStrategy":"validation","validationCode":"function assertValidSearchInput(query: string, options: SearchOptions = {}): void {\n  if (typeof query !== 'string' || query.trim().length === 0) {\n    throw new Error('search query must be a non-empty string');\n  }\n  if (query.length > 400) throw new Error('search query too long');\n}","typeGuard":"function isNonEmptyQuery(q: unknown): q is string {\n  return typeof q === 'string' && q.trim().length > 0;\n}","tryCatchPattern":"try {\n  const result = await client.search.google(query);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('bad request:')) {\n    // log the Bright Data reason embedded in the message for debugging\n    logger.warn({ reason: err.message }, 'brightdata rejected request');\n    return null; // or rethrow after sanitizing inputs\n  }\n  throw err;\n}","preventionTips":["Always trim and length-check user-supplied queries before calling the client.","Validate all option values (language, country, etc.) against expected formats.","Never interpolate raw user input into request options without sanitization.","Log the response body included in the message to learn Bright Data's exact complaint."],"tags":["http-400","validation","network","bad-request"],"backgroundTag":"bad-request","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}