Skip to content

Commit 444d144

Browse files
authored
Merge pull request #38 from pattern-tech/feat/auth-reason
chore: improve error messages
2 parents a218233 + c67299f commit 444d144

5 files changed

Lines changed: 85 additions & 19 deletions

File tree

app/(auth)/adapter.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ export const getAllWorkspaces = async (
4444
`Fetching workspaces failed with error code ${allWorkspacesResponse.status}`,
4545
);
4646
} catch (error) {
47-
return Err(extractErrorMessageOrDefault(error));
47+
return Err(
48+
extractErrorMessageOrDefault(
49+
error,
50+
'Unknown error while fetching workspaces',
51+
),
52+
);
4853
}
4954
};
5055

@@ -76,7 +81,12 @@ export const createWorkspace = async (
7681
}
7782
return Err(`Creating workspace failed with error code ${response.status}`);
7883
} catch (error) {
79-
return Err(extractErrorMessageOrDefault(error));
84+
return Err(
85+
extractErrorMessageOrDefault(
86+
error,
87+
'Unknown error while creating workspace',
88+
),
89+
);
8090
}
8191
};
8292

@@ -106,7 +116,12 @@ export const getAllProjects = async (
106116
`Fetching projects failed with error code ${allProjectsResponse.status}`,
107117
);
108118
} catch (error) {
109-
return Err(extractErrorMessageOrDefault(error));
119+
return Err(
120+
extractErrorMessageOrDefault(
121+
error,
122+
'Unknown error while fetching projects',
123+
),
124+
);
110125
}
111126
};
112127

@@ -140,6 +155,11 @@ export const createProjectInWorkspace = async (
140155
}
141156
return Err(`Creating project failed with error code ${response.status}`);
142157
} catch (error) {
143-
return Err(extractErrorMessageOrDefault(error));
158+
return Err(
159+
extractErrorMessageOrDefault(
160+
error,
161+
'Unknown error while creating project',
162+
),
163+
);
144164
}
145165
};

app/(chat)/adapter.ts

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export const getConversation = async (
5353
`Fetching conversation failed with error code ${conversationResponse.status}`,
5454
);
5555
} catch (error) {
56-
return Err(extractErrorMessageOrDefault(error));
56+
return Err(
57+
extractErrorMessageOrDefault(
58+
error,
59+
'Unknown error while fetching conversation',
60+
),
61+
);
5762
}
5863
};
5964

@@ -91,7 +96,12 @@ export const getConversationMessages = async (
9196
`Fetching conversation messages failed with error code ${conversationResponse.status}`,
9297
);
9398
} catch (error) {
94-
return Err(extractErrorMessageOrDefault(error));
99+
return Err(
100+
extractErrorMessageOrDefault(
101+
error,
102+
'Unknown error while fetching conversation messages',
103+
),
104+
);
95105
}
96106
};
97107

@@ -138,7 +148,12 @@ export const createConversation = async (
138148
`Creating conversation failed with error code ${conversationResponse.status}`,
139149
);
140150
} catch (error) {
141-
return Err(extractErrorMessageOrDefault(error));
151+
return Err(
152+
extractErrorMessageOrDefault(
153+
error,
154+
'Unknown error while creating conversation',
155+
),
156+
);
142157
}
143158
};
144159

@@ -185,7 +200,12 @@ export const sendMessage = async (
185200
`Sending message failed with error code ${messageResponse.status}`,
186201
);
187202
} catch (error) {
188-
return Err(extractErrorMessageOrDefault(error));
203+
return Err(
204+
extractErrorMessageOrDefault(
205+
error,
206+
'Unknown error while sending message',
207+
),
208+
);
189209
}
190210
};
191211

@@ -228,11 +248,26 @@ export const sendMessageStreamed = async (
228248
: Err('Message was sent but stream object is null');
229249
}
230250

251+
/**
252+
* This error is specifically handled because the user should know the
253+
* reason, so that they can top up their MOR stake if needed
254+
*/
255+
if (messageResponse.status === 429) {
256+
return Err(
257+
'You have used all your daily prompt credits. Please top up your MOR stake to continue, or wait until tomorrow.',
258+
);
259+
}
260+
231261
return Err(
232262
`Sending message failed with error code ${messageResponse.status}`,
233263
);
234264
} catch (error) {
235-
return Err(extractErrorMessageOrDefault(error));
265+
return Err(
266+
extractErrorMessageOrDefault(
267+
error,
268+
'Unknown error while sending streamed message',
269+
),
270+
);
236271
}
237272
};
238273

@@ -267,7 +302,12 @@ export const getAllConversations = async (
267302
`Fetching projects failed with error code ${allConversationsResponse.status}`,
268303
);
269304
} catch (error) {
270-
return Err(extractErrorMessageOrDefault(error));
305+
return Err(
306+
extractErrorMessageOrDefault(
307+
error,
308+
'Unknown error while fetching all conversations',
309+
),
310+
);
271311
}
272312
};
273313

@@ -311,6 +351,11 @@ export const renameConversation = async (
311351
`Renaming conversation failed with error code ${renameResponse.status}`,
312352
);
313353
} catch (error) {
314-
return Err(extractErrorMessageOrDefault(error));
354+
return Err(
355+
extractErrorMessageOrDefault(
356+
error,
357+
'Unknown error while renaming conversation',
358+
),
359+
);
315360
}
316361
};

app/(chat)/api/chat/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ export async function POST(request: Request) {
7575
});
7676
},
7777
onError: (error) => {
78-
return extractErrorMessageOrDefault(error);
78+
/**
79+
* We may get an error object when calling message API, or an error string
80+
* when transforming the stream
81+
*/
82+
return typeof error === 'string'
83+
? error
84+
: extractErrorMessageOrDefault(error);
7985
},
8086
});
8187
}

components/chat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function Chat({
4848
mutate('/api/history');
4949
},
5050
onError: (error) => {
51-
toast.error('An error occured, please try again!');
51+
toast.error(error.message);
5252
},
5353
});
5454

lib/ai/pattern-model.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import type {
1111
ToolStartEvent,
1212
} from '@/lib/ai/types';
1313

14-
import { extractErrorMessageOrDefault } from '../utils';
15-
1614
const textDecoder = new TextDecoder();
1715

1816
export class PatternModel implements LanguageModelV1 {
@@ -115,10 +113,7 @@ export class PatternModel implements LanguageModelV1 {
115113
} catch (error) {
116114
controller.enqueue({
117115
type: 'error',
118-
error: extractErrorMessageOrDefault(
119-
error,
120-
'An unknown error occurred when transforming response chunk',
121-
),
116+
error: 'Cannot parse chunk due to corrupted data or invalid JSON',
122117
});
123118
}
124119
} else {

0 commit comments

Comments
 (0)