Initial commit: EGBE chatbot template
Stripped from vercel/chatbot (Apache 2.0): - Dropped @vercel/* packages and AI Gateway - Removed artifacts feature (code/text/sheet/image side panel) - Switched AI provider to @ai-sdk/openai-compatible -> EGBE LiteLLM - Replaced Vercel Blob upload with data URLs - Dropped Redis resumable streams and rate limiter (in-memory now) - Added Dockerfile (Next.js standalone) + entrypoint that runs migrations - Wired DATABASE_URL, EGBE_AI_API_URL/KEY, NEXT_PUBLIC_BASE_URL for app-deploy.sh
This commit is contained in:
commit
3e21c2334c
129 changed files with 21913 additions and 0 deletions
119
lib/errors.ts
Normal file
119
lib/errors.ts
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
export type ErrorType =
|
||||
| "bad_request"
|
||||
| "unauthorized"
|
||||
| "forbidden"
|
||||
| "not_found"
|
||||
| "rate_limit"
|
||||
| "offline";
|
||||
|
||||
export type Surface =
|
||||
| "chat"
|
||||
| "auth"
|
||||
| "api"
|
||||
| "stream"
|
||||
| "database"
|
||||
| "history"
|
||||
| "vote";
|
||||
|
||||
export type ErrorCode = `${ErrorType}:${Surface}`;
|
||||
|
||||
export type ErrorVisibility = "response" | "log" | "none";
|
||||
|
||||
export const visibilityBySurface: Record<Surface, ErrorVisibility> = {
|
||||
database: "log",
|
||||
chat: "response",
|
||||
auth: "response",
|
||||
stream: "response",
|
||||
api: "response",
|
||||
history: "response",
|
||||
vote: "response",
|
||||
};
|
||||
|
||||
export class ChatbotError extends Error {
|
||||
type: ErrorType;
|
||||
surface: Surface;
|
||||
statusCode: number;
|
||||
|
||||
constructor(errorCode: ErrorCode, cause?: string) {
|
||||
super();
|
||||
|
||||
const [type, surface] = errorCode.split(":");
|
||||
|
||||
this.type = type as ErrorType;
|
||||
this.cause = cause;
|
||||
this.surface = surface as Surface;
|
||||
this.message = getMessageByErrorCode(errorCode);
|
||||
this.statusCode = getStatusCodeByType(this.type);
|
||||
}
|
||||
|
||||
toResponse() {
|
||||
const code: ErrorCode = `${this.type}:${this.surface}`;
|
||||
const visibility = visibilityBySurface[this.surface];
|
||||
|
||||
const { message, cause, statusCode } = this;
|
||||
|
||||
if (visibility === "log") {
|
||||
console.error({
|
||||
code,
|
||||
message,
|
||||
cause,
|
||||
});
|
||||
|
||||
return Response.json(
|
||||
{ code: "", message: "Something went wrong. Please try again later." },
|
||||
{ status: statusCode }
|
||||
);
|
||||
}
|
||||
|
||||
return Response.json({ code, message, cause }, { status: statusCode });
|
||||
}
|
||||
}
|
||||
|
||||
export function getMessageByErrorCode(errorCode: ErrorCode): string {
|
||||
if (errorCode.includes("database")) {
|
||||
return "An error occurred while executing a database query.";
|
||||
}
|
||||
|
||||
switch (errorCode) {
|
||||
case "bad_request:api":
|
||||
return "The request couldn't be processed. Please check your input and try again.";
|
||||
|
||||
case "unauthorized:auth":
|
||||
return "You need to sign in before continuing.";
|
||||
case "forbidden:auth":
|
||||
return "Your account does not have access to this feature.";
|
||||
|
||||
case "rate_limit:chat":
|
||||
return "You've reached the message limit. Come back in 1 hour to continue chatting.";
|
||||
case "not_found:chat":
|
||||
return "The requested chat was not found. Please check the chat ID and try again.";
|
||||
case "forbidden:chat":
|
||||
return "This chat belongs to another user. Please check the chat ID and try again.";
|
||||
case "unauthorized:chat":
|
||||
return "You need to sign in to view this chat. Please sign in and try again.";
|
||||
case "offline:chat":
|
||||
return "We're having trouble sending your message. Please check your internet connection and try again.";
|
||||
|
||||
default:
|
||||
return "Something went wrong. Please try again later.";
|
||||
}
|
||||
}
|
||||
|
||||
function getStatusCodeByType(type: ErrorType) {
|
||||
switch (type) {
|
||||
case "bad_request":
|
||||
return 400;
|
||||
case "unauthorized":
|
||||
return 401;
|
||||
case "forbidden":
|
||||
return 403;
|
||||
case "not_found":
|
||||
return 404;
|
||||
case "rate_limit":
|
||||
return 429;
|
||||
case "offline":
|
||||
return 503;
|
||||
default:
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue