Implementation:Langgenius Dify Error Parser
| Knowledge Sources | |
|---|---|
| Domains | Frontend, Utilities |
| Last Updated | 2026-02-12 07:00 GMT |
Overview
Extracts human-readable error messages from nested plugin invocation error structures.
Description
This utility provides the parsePluginErrorMessage function, which handles the common pattern where Dify plugin errors are wrapped inside a PluginInvokeError JSON string embedded within a larger error message. The function accepts either a Response object (from fetch/ky) or a standard error object, extracts the raw message, then applies a regex to locate and parse the embedded JSON payload. It returns the innermost message field when available, falls back to error_type, and ultimately returns the raw message if no nested structure is found. This ensures users see meaningful, actionable error messages rather than raw JSON or request IDs.
Usage
Use parsePluginErrorMessage in error handling paths for plugin-related API calls, particularly when the backend returns errors in the format req_id: xxx PluginInvokeError: {"message":"..."}. This is typically called in catch blocks or error response handlers.
Code Reference
Source Location
- Repository: Langgenius_Dify
- File: web/utils/error-parser.ts
- Lines: 1-52
Signature
export const parsePluginErrorMessage = async (error: any): Promise<string>
Import
import { parsePluginErrorMessage } from '@/utils/error-parser'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| error | any |
Yes | An error object; can be a fetch Response object, a standard Error, or any object with a message property
|
Outputs
| Name | Type | Description |
|---|---|---|
| (return value) | Promise<string> |
The extracted human-readable error message; defaults to "Unknown error" if nothing can be parsed |
Usage Examples
Parse a Plugin Invoke Error from a Response
import { parsePluginErrorMessage } from '@/utils/error-parser'
try {
const response = await fetch('/api/plugin/invoke', { method: 'POST', body: payload })
if (!response.ok) {
const message = await parsePluginErrorMessage(response)
// message: "Bad credentials" (extracted from nested JSON)
showError(message)
}
} catch (err) {
const message = await parsePluginErrorMessage(err)
showError(message)
}
Parse a Standard Error Object
import { parsePluginErrorMessage } from '@/utils/error-parser'
const error = new Error('req_id: abc123 PluginInvokeError: {"message":"API rate limit exceeded","error_type":"RateLimitError"}')
const message = await parsePluginErrorMessage(error)
// message: "API rate limit exceeded"