refactor: nodejs sdk (#30036)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
yyh
2025-12-23 23:24:38 +08:00
committed by GitHub
parent de021ff3e0
commit 4d48791f3c
49 changed files with 7901 additions and 701 deletions

View File

@ -0,0 +1,37 @@
import { describe, expect, it } from "vitest";
import {
APIError,
AuthenticationError,
DifyError,
FileUploadError,
NetworkError,
RateLimitError,
TimeoutError,
ValidationError,
} from "./dify-error";
describe("Dify errors", () => {
it("sets base error fields", () => {
const err = new DifyError("base", {
statusCode: 400,
responseBody: { message: "bad" },
requestId: "req",
retryAfter: 1,
});
expect(err.name).toBe("DifyError");
expect(err.statusCode).toBe(400);
expect(err.responseBody).toEqual({ message: "bad" });
expect(err.requestId).toBe("req");
expect(err.retryAfter).toBe(1);
});
it("creates specific error types", () => {
expect(new APIError("api").name).toBe("APIError");
expect(new AuthenticationError("auth").name).toBe("AuthenticationError");
expect(new RateLimitError("rate").name).toBe("RateLimitError");
expect(new ValidationError("val").name).toBe("ValidationError");
expect(new NetworkError("net").name).toBe("NetworkError");
expect(new TimeoutError("timeout").name).toBe("TimeoutError");
expect(new FileUploadError("upload").name).toBe("FileUploadError");
});
});

View File

@ -0,0 +1,75 @@
export type DifyErrorOptions = {
statusCode?: number;
responseBody?: unknown;
requestId?: string;
retryAfter?: number;
cause?: unknown;
};
export class DifyError extends Error {
statusCode?: number;
responseBody?: unknown;
requestId?: string;
retryAfter?: number;
constructor(message: string, options: DifyErrorOptions = {}) {
super(message);
this.name = "DifyError";
this.statusCode = options.statusCode;
this.responseBody = options.responseBody;
this.requestId = options.requestId;
this.retryAfter = options.retryAfter;
if (options.cause) {
(this as { cause?: unknown }).cause = options.cause;
}
}
}
export class APIError extends DifyError {
constructor(message: string, options: DifyErrorOptions = {}) {
super(message, options);
this.name = "APIError";
}
}
export class AuthenticationError extends APIError {
constructor(message: string, options: DifyErrorOptions = {}) {
super(message, options);
this.name = "AuthenticationError";
}
}
export class RateLimitError extends APIError {
constructor(message: string, options: DifyErrorOptions = {}) {
super(message, options);
this.name = "RateLimitError";
}
}
export class ValidationError extends APIError {
constructor(message: string, options: DifyErrorOptions = {}) {
super(message, options);
this.name = "ValidationError";
}
}
export class NetworkError extends DifyError {
constructor(message: string, options: DifyErrorOptions = {}) {
super(message, options);
this.name = "NetworkError";
}
}
export class TimeoutError extends DifyError {
constructor(message: string, options: DifyErrorOptions = {}) {
super(message, options);
this.name = "TimeoutError";
}
}
export class FileUploadError extends DifyError {
constructor(message: string, options: DifyErrorOptions = {}) {
super(message, options);
this.name = "FileUploadError";
}
}