@@ -6,8 +6,11 @@ import {
66 UseMutationResult
77} from '@tanstack/react-query' ;
88
9- import { sendFetchRequest , HTTPError } from '@/utils' ;
10- import { toHttpError } from '@/utils/errorHandling' ;
9+ import { sendFetchRequest } from '@/utils' ;
10+ import {
11+ getResponseJsonOrError ,
12+ throwResponseNotOkError
13+ } from './queryUtils' ;
1114
1215export type NGLink = {
1316 short_key : string ;
@@ -51,26 +54,24 @@ export const ngLinkQueryKeys = {
5154} ;
5255
5356const fetchNGLinks = async ( signal ?: AbortSignal ) : Promise < NGLink [ ] > => {
54- try {
55- const response = await sendFetchRequest (
56- '/api/neuroglancer/nglinks' ,
57- 'GET' ,
58- undefined ,
59- { signal }
60- ) ;
61- if ( response . status === 404 ) {
62- return [ ] ;
63- }
64- if ( ! response . ok ) {
65- throw await toHttpError ( response ) ;
66- }
67- const data = ( await response . json ( ) ) as NGLinksResponse ;
57+ const response = await sendFetchRequest (
58+ '/api/neuroglancer/nglinks' ,
59+ 'GET' ,
60+ undefined ,
61+ { signal }
62+ ) ;
63+ const data = ( await getResponseJsonOrError ( response ) ) as NGLinksResponse ;
64+
65+ if ( response . ok ) {
6866 return data . links ?? [ ] ;
69- } catch ( error ) {
70- if ( error instanceof HTTPError && error . responseCode === 404 ) {
71- return [ ] ;
72- }
73- throw error ;
67+ }
68+
69+ // Handle error responses
70+ if ( response . status === 404 ) {
71+ // Not an error, just no links available
72+ return [ ] ;
73+ } else {
74+ throwResponseNotOkError ( response , data ) ;
7475 }
7576} ;
7677
@@ -95,10 +96,12 @@ export function useCreateNGLinkMutation(): UseMutationResult<
9596 'POST' ,
9697 payload
9798 ) ;
99+ const data = ( await getResponseJsonOrError ( response ) ) as NGLinkResponse ;
100+
98101 if ( ! response . ok ) {
99- throw await toHttpError ( response ) ;
102+ throwResponseNotOkError ( response , data ) ;
100103 }
101- return ( await response . json ( ) ) as NGLinkResponse ;
104+ return data ;
102105 } ,
103106 onSuccess : ( ) => {
104107 queryClient . invalidateQueries ( {
@@ -122,10 +125,12 @@ export function useUpdateNGLinkMutation(): UseMutationResult<
122125 'PUT' ,
123126 { url : payload . url , title : payload . title }
124127 ) ;
128+ const data = ( await getResponseJsonOrError ( response ) ) as NGLinkResponse ;
129+
125130 if ( ! response . ok ) {
126- throw await toHttpError ( response ) ;
131+ throwResponseNotOkError ( response , data ) ;
127132 }
128- return ( await response . json ( ) ) as NGLinkResponse ;
133+ return data ;
129134 } ,
130135 onSuccess : ( ) => {
131136 queryClient . invalidateQueries ( {
@@ -148,8 +153,10 @@ export function useDeleteNGLinkMutation(): UseMutationResult<
148153 `/api/neuroglancer/nglinks/${ encodeURIComponent ( shortKey ) } ` ,
149154 'DELETE'
150155 ) ;
156+ const data = await getResponseJsonOrError ( response ) ;
157+
151158 if ( ! response . ok ) {
152- throw await toHttpError ( response ) ;
159+ throwResponseNotOkError ( response , data ) ;
153160 }
154161 } ,
155162 onSuccess : ( ) => {
0 commit comments