Skip to main content

ParseError

Thrown when attempting to parse a non-JSON response.

Import

import { ParseError } from '@activade/droid-sdk';

Properties

rawText

readonly rawText: string

The original text that failed to parse.

Usage

import { z } from 'zod';

const Schema = z.object({ name: z.string() });

try {
const data = result.parse(Schema);
} catch (error) {
if (error instanceof ParseError) {
console.error('Not valid JSON');
console.error('Raw response:', error.rawText);
}
}

Distinguishing Parse Errors

import { ParseError } from '@activade/droid-sdk';
import { z } from 'zod';

try {
const data = result.parse(MySchema);
} catch (error) {
if (error instanceof ParseError) {
// Response was not valid JSON
console.error('JSON syntax error:', error.message);
console.error('Response was:', error.rawText);
} else if (error instanceof z.ZodError) {
// JSON was valid but didn't match schema
console.error('Validation errors:', error.issues);
}
}

Using tryParse Instead

For optional JSON parsing, use tryParse() to avoid exceptions:

const data = result.tryParse(MySchema);

if (data) {
console.log('Parsed:', data);
} else {
console.log('Response was not valid JSON');
console.log('Raw response:', result.finalResponse);
}

Prompting for Better JSON

If you frequently get ParseError, improve your prompts:

// Better prompt for JSON output
const result = await droid.exec(`
Analyze the file and return ONLY a JSON object with this structure:
{
"lines": number,
"functions": number,
"complexity": "low" | "medium" | "high"
}

Return ONLY the JSON, no other text.
`);