mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
Replace strip-json-comments with jsonc-parser for better error reporting
This commit is contained in:
1 parent
e1890d0554
commit
e1a4a70d3d
13 files changed
+132
-34
No files matched your search
@@ -8,13 +8,34 @@ interface FetchWrapperOptions extends RequestInit {
|
||||
params?: Record<string, string | number | boolean | undefined>
|
||||
}
|
||||
|
||||
function formatDetails(details: unknown): string | undefined {
|
||||
if (Array.isArray(details)) {
|
||||
return details
|
||||
.map((d) => {
|
||||
if (typeof d !== 'object' || d === null) return null
|
||||
const path = Array.isArray((d as Record<string, unknown>).path)
|
||||
? ((d as Record<string, unknown>).path as string[])
|
||||
: undefined
|
||||
const message = typeof (d as Record<string, unknown>).message === 'string'
|
||||
? (d as Record<string, unknown>).message as string
|
||||
: undefined
|
||||
return path?.length ? `${path.join('.')}: ${message}` : message
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join('; ')
|
||||
}
|
||||
if (typeof details === 'string') return details
|
||||
return undefined
|
||||
}
|
||||
|
||||
async function handleResponse(response: Response): Promise<never> {
|
||||
const data: ApiErrorResponse = await response.json().catch(() => ({ error: 'An error occurred' }))
|
||||
const detail = data.detail || formatDetails(data.details)
|
||||
throw new FetchError(
|
||||
data.error || 'Request failed',
|
||||
response.status,
|
||||
data.code,
|
||||
data.detail
|
||||
detail
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Loader2 } from 'lucide-react'
|
||||
import { Dialog, DialogContent, DialogHeader, DialogFooter, DialogTitle } from '@/components/ui/dialog'
|
||||
import type { OpenCodeConfig } from '@/api/types/settings'
|
||||
import { parseJsonc } from '@/lib/jsonc'
|
||||
import { FetchError } from '@/api/fetchWrapper'
|
||||
|
||||
interface OpenCodeConfigEditorProps {
|
||||
config: OpenCodeConfig | null
|
||||
@@ -44,28 +45,46 @@ export function OpenCodeConfigEditor({
|
||||
if (!config) return
|
||||
|
||||
try {
|
||||
const parsedContent = parseJsonc<Record<string, unknown>>(editConfigContent)
|
||||
|
||||
const forbiddenFields = ['id', 'createdAt', 'updatedAt']
|
||||
const foundForbidden = forbiddenFields.filter(field => field in parsedContent)
|
||||
if (foundForbidden.length > 0) {
|
||||
throw new Error(`Invalid fields found: ${foundForbidden.join(', ')}. These fields are managed automatically.`)
|
||||
}
|
||||
|
||||
parseJsonc<Record<string, unknown>>(editConfigContent)
|
||||
await onUpdate(editConfigContent)
|
||||
onClose()
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
const match = error.message.match(/line (\d+)/i)
|
||||
const line = match ? parseInt(match[1]) : null
|
||||
const lineMatch = error.message.match(/line\s+(\d+)/i)
|
||||
const line = lineMatch ? parseInt(lineMatch[1]) : null
|
||||
setEditErrorLine(line)
|
||||
setEditError('Invalid JSON/JSONC format')
|
||||
if (line && editTextareaRef.current) {
|
||||
highlightErrorLine(editTextareaRef.current, line)
|
||||
}
|
||||
setEditError(`Invalid JSON/JSONC: ${error.message}`)
|
||||
} else if (error instanceof FetchError) {
|
||||
setEditError(error.detail || error.message)
|
||||
} else if (error instanceof Error) {
|
||||
setEditError(error.message)
|
||||
} else {
|
||||
setEditError('Failed to save. Please check your changes and try again.')
|
||||
setEditError('Failed to save configuration')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const highlightErrorLine = (textarea: HTMLTextAreaElement, line: number) => {
|
||||
const lines = textarea.value.split('\n')
|
||||
if (line > lines.length) return
|
||||
|
||||
let charIndex = 0
|
||||
for (let i = 0; i < line - 1; i++) {
|
||||
charIndex += lines[i].length + 1
|
||||
}
|
||||
|
||||
textarea.focus()
|
||||
textarea.setSelectionRange(charIndex, charIndex + lines[line - 1].length)
|
||||
|
||||
// Scroll to make the error line visible
|
||||
const lineHeight = textarea.scrollHeight / lines.length
|
||||
const targetPosition = lineHeight * (line - 1)
|
||||
textarea.scrollTop = targetPosition - textarea.clientHeight / 2 + lineHeight / 2
|
||||
}
|
||||
|
||||
if (!config) return null
|
||||
|
||||
return (
|
||||
@@ -87,7 +106,7 @@ export function OpenCodeConfigEditor({
|
||||
setEditError('')
|
||||
setEditErrorLine(null)
|
||||
}}
|
||||
className="flex-1 font-mono text-[16px] sm:text-xs md:text-sm resize-none h-full rounded-none sm:rounded-md"
|
||||
className={`flex-1 font-mono text-[16px] sm:text-xs md:text-sm resize-none h-full rounded-none sm:rounded-md ${editErrorLine ? 'error-highlight' : ''}`}
|
||||
/>
|
||||
{editError && (
|
||||
<div className="absolute bottom-0 left-0 right-0 bg-background/95 border-t p-2 sm:p-3">
|
||||
|
||||
@@ -374,3 +374,24 @@ body {
|
||||
.animate-progress {
|
||||
animation: progress 2s ease-in-out infinite;
|
||||
}
|
||||
|
||||
/* Error line highlighting - red-orange selection color */
|
||||
.error-highlight::selection {
|
||||
background-color: #fecaca;
|
||||
color: #991b1b;
|
||||
}
|
||||
|
||||
.dark .error-highlight::selection {
|
||||
background-color: #7f1d1d;
|
||||
color: #fecaca;
|
||||
}
|
||||
|
||||
.error-highlight::-moz-selection {
|
||||
background-color: #fecaca;
|
||||
color: #991b1b;
|
||||
}
|
||||
|
||||
.dark .error-highlight::-moz-selection {
|
||||
background-color: #7f1d1d;
|
||||
color: #fecaca;
|
||||
}
|
||||
@@ -1,12 +1,4 @@
|
||||
import stripJsonComments from 'strip-json-comments'
|
||||
|
||||
export function parseJsonc<T = unknown>(content: string): T {
|
||||
try {
|
||||
return JSON.parse(stripJsonComments(content)) as T
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to parse JSONC: ${e instanceof Error ? e.message : String(e)}`)
|
||||
}
|
||||
}
|
||||
export { parseJsonc } from '@opencode-manager/shared/utils'
|
||||
|
||||
export function hasJsoncComments(content: string): boolean {
|
||||
return content.split('\n').some(line => {
|
||||
|
||||
Reference in new issue
Block a user