[Improvement] Add generic error types to useQuery of @powersync/react
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- react, typescript
- Ambito
- frontend
Direzione di ricerca
Start with the QueryResult type and useQuery signature in packages/react/src/hooks/useQuery.ts, then the CompilableQuery interface in packages/common/src/types/types.ts. The proposal adds an error type parameter E that defaults to Error, but the issue does not say where runtime errors are thrown or how E would reach them, so settle that design first. Done means useQuery and QueryResult accept an optional E without breaking existing callers, and the TypeScript types check. The thread has been idle for about 590 days and has no maintainer decision, so confirm the approach with maintainers before writing code.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
Currently, the PowerSync query system uses a generic Error type for error handling in QueryResult https://github.com/powersync-ja/powersync-js/blob/main/packages/react/src/hooks/useQuery.ts#L9 and https://github.com/powersync-ja/powersync-js/blob/main/packages/common/src/types/types.ts.
This makes it difficult to handle specific types of errors in a type-safe manner when using TypeScript, forcing developers to use type assertions or broadly catch all errors without type discrimination.
Proposed Solution
Add generic error type support to the query system, allowing developers to specify and handle specific error types with full TypeScript support.
Type Changes
// In packages/common/src/types/types.ts
export interface CompilableQuery<T, E = Error> {
execute(): Promise<T[]>;
compile(): CompiledQuery;
}
// In packages/react/src/hooks/useQuery.ts
export type QueryResult<T, E = Error> = {
data: T[];
isLoading: boolean;
isFetching: boolean;
error: E | undefined;
refresh?: () => Promise<void>;
};
export const useQuery = <T = any, E = Error>(
query: string | CompilableQuery<T, E>,
parameters?: any[],
options?: AdditionalOptions
): QueryResult<T, E> => {
// Implementation remains largely the same
// Just needs to properly type the error handling
};
Usage Example
// Define your error types
interface PowerSyncError {
type: string;
message: string;
}
interface SQLError extends PowerSyncError {
type: 'SQLError';
code: number;
}
interface ValidationError extends PowerSyncError {
type: 'ValidationError';
fields: string[];
}
type AppError = SQLError | ValidationError;
// In your React component
function MyComponent() {
const { data, error } = useQuery<User, AppError>('SELECT * FROM users');
if (error) {
if (error.type === 'SQLError') {
// TypeScript knows this is SQLError
console.error(`Database error ${error.code}: ${error.message}`);
} else if (error.type === 'ValidationError') {
// TypeScript knows this is ValidationError
console.error(`Invalid fields: ${error.fields.join(', ')}`);
}
}
return // ...
}
Benefits
- Type-safe error handling
- Better developer experience with IDE support
- Clearer error handling patterns
- No breaking changes for existing code (defaults to current Error type)
- Enables custom error types for different use cases
Implementation Notes
- This change is backward compatible as it defaults to the current
Errortype - Minimal changes required to existing codebase
- Lingua principale
- TypeScript
- Stelle
- 729
- Fork
- 96
- Merge medio
- 2g 1h
- PR unite (30g)
- 26
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di powersync-ja/powersync-js
-
Sync loop stops with a TypeError when an iteration rejects with no value after a connection changeAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
powersync-ja/powersync-js#1163 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
powersync-ja/powersync-js#1159 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Differential watched queries ignore pure reorders (same rows, new ORDER BY) so stale order is servedAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
powersync-ja/powersync-js#1156 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 54/100
powersync-ja/powersync-js#1164 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
powersync-ja/powersync-js#1161 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di powersync-ja/powersync-js
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
braintrustdata/braintrust-sdk-javascript#2659 ·
I maintainer di solito rispondono entro 1 giorno
-
body-parser 2.3.0 in the published ai-dial-chat-bff image is vulnerable (CVE-2026-111181, HIGH)Forse già presa @illya-havsiyevych l’ha presa oggi. Apertabug
Difficoltà 1/5 1-3 ore Idoneità per principianti 87/100
epam/ai-dial-chat#9566 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
I maintainer di solito rispondono entro 1 giorno
-
はさみアイコンを修正するApertabug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 68/100
-
resource.reload() while params is in error switches the resource to 'idle' instead of retryingApertaarea: core
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno