MCPcopy Create free account
hub / github.com/pollinations/pollinations / parseApiError

Function parseApiError

packages/mcp/src/utils/coreUtils.js:242–282  ·  view source on GitHub ↗
(status, errorText)

Source from the content-addressed store, hash-verified

240 * @returns {string} - User-friendly error message
241 */
242export function parseApiError(status, errorText) {
243 let parsed = null;
244 try {
245 parsed = JSON.parse(errorText);
246 } catch {}
247 const errorMessage =
248 parsed?.error?.message || parsed?.message || parsed?.error || errorText;
249 switch (status) {
250 case 400:
251 if (
252 errorMessage.toLowerCase().includes("content moderation") ||
253 errorMessage.toLowerCase().includes("safety") ||
254 errorMessage.toLowerCase().includes("blocked")
255 ) {
256 return `Content blocked by safety filters. Try rephrasing your prompt or disable 'safe' mode if appropriate.`;
257 }
258 if (errorMessage.toLowerCase().includes("invalid model")) {
259 return `Invalid model specified. Use listImageModels or listTextModels to see available options.`;
260 }
261 return `Bad request: ${errorMessage}`;
262 case 401:
263 return `Authentication failed. Please set a valid API key using setApiKey. Get your key at https://enter.pollinations.ai`;
264 case 403:
265 return `Access forbidden. Your API key may not have permission for this operation.`;
266 case 404:
267 return `Resource not found. The requested endpoint or model may not exist.`;
268 case 429:
269 return (
270 `Rate limited. You're making too many requests. ` +
271 `If using pk_ (publishable) key, consider upgrading to sk_ (secret) key for higher limits.`
272 );
273 case 500:
274 return `Server error: ${errorMessage}. Please try again later.`;
275 case 502:
276 case 503:
277 case 504:
278 return `Service temporarily unavailable. Please try again in a few moments.`;
279 default:
280 return `Request failed (${status}): ${errorMessage}`;
281 }
282}

Callers 5

webSearchFunction · 0.90
fetchJsonWithAuthFunction · 0.70
chatWithMediaFunction · 0.70
postChatCompletionFunction · 0.70
fetchBinaryWithAuthFunction · 0.70

Calls 1

parseMethod · 0.80

Tested by

no test coverage detected