MCPcopy Create free account
hub / github.com/omkarcloud/botasaurus / createHelpfulJsonError

Function createHelpfulJsonError

botasaurus-controls/src/index.ts:441–470  ·  view source on GitHub ↗
(e: any, trimmedValue: string)

Source from the content-addressed store, hash-verified

439 return value;
440}
441function createHelpfulJsonError(e: any, trimmedValue: string) {
442 const errorMessage = e.message || 'Invalid JSON'
443
444 // Extract position info if available
445 const positionMatch = errorMessage.match(/position\s+(\d+)/i)
446 const position = positionMatch ? parseInt(positionMatch[1], 10) : null
447
448 let helpfulMessage = 'Invalid JSON: '
449
450 // Check for common mistakes
451 if (trimmedValue.includes("'") && !trimmedValue.includes('"')) {
452 helpfulMessage += "Use double quotes (\") instead of single quotes (') for strings."
453 } else if (/,\s*[}\]]/.test(trimmedValue)) {
454 helpfulMessage += "Trailing comma found. Remove the comma before the closing bracket."
455 } else if (errorMessage.includes('Unexpected token')) {
456 if (position !== null) {
457 const contextStart = Math.max(0, position - 10)
458 const contextEnd = Math.min(trimmedValue.length, position + 10)
459 const context = trimmedValue.substring(contextStart, contextEnd)
460 helpfulMessage += `Unexpected character near position ${position}: "...${context}..."`
461 } else {
462 helpfulMessage += errorMessage
463 }
464 } else if (errorMessage.includes('Unexpected end')) {
465 helpfulMessage += "JSON is incomplete. Check for missing closing brackets or quotes."
466 } else {
467 helpfulMessage += errorMessage
468 }
469 return helpfulMessage
470}
471function parseJSON(value: any): { parsed: any; error: string | null } {
472 if (typeof value !== 'string') {
473 return { parsed: value, error: null };

Callers 1

parseJSONFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected