MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / handleResponse

Method handleResponse

frontend/src/lib/api/apiClient.ts:60–102  ·  view source on GitHub ↗
(response: Response, originalRequest?: { endpoint: string; options: RequestOptions })

Source from the content-addressed store, hash-verified

58 }
59
60 private async handleResponse<T>(response: Response, originalRequest?: { endpoint: string; options: RequestOptions }): Promise<T> {
61 if (!response.ok) {
62 if (response.status === 401 && originalRequest && !originalRequest.options.skipAuth) {
63 const retryCount = originalRequest.options._retryCount || 0;
64
65 // Prevent infinite loops - only retry once
66 if (retryCount >= 1) {
67 throw new Error('Session expired. Please log in again.');
68 }
69
70 try {
71 // Attempt to refresh the token
72 await this.refreshToken();
73
74 // Retry the original request with incremented retry count
75 const retryOptions = {
76 ...originalRequest.options,
77 _retryCount: retryCount + 1
78 };
79 return this.request<T>(originalRequest.endpoint, retryOptions);
80 } catch (refreshError) {
81 throw new Error('Session expired. Please log in again.');
82 }
83 }
84
85 let errorMessage = `Request failed with status ${response.status}`;
86 try {
87 const errorData = await response.json();
88 errorMessage = errorData.message || errorMessage;
89 } catch {
90 // Ignore JSON parse error
91 }
92
93 throw new Error(errorMessage);
94 }
95
96 // Handle empty responses
97 if (response.status === 204 || response.headers.get('content-length') === '0') {
98 return {} as T;
99 }
100
101 return response.json();
102 }
103
104 async request<T>(
105 endpoint: string,

Callers

nothing calls this directly

Calls 3

refreshTokenMethod · 0.95
requestMethod · 0.95
getMethod · 0.45

Tested by

no test coverage detected