mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 01:18:05 +08:00
refactor: nodejs sdk (#30036)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
37
sdks/nodejs-client/src/errors/dify-error.test.js
Normal file
37
sdks/nodejs-client/src/errors/dify-error.test.js
Normal 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");
|
||||
});
|
||||
});
|
||||
75
sdks/nodejs-client/src/errors/dify-error.ts
Normal file
75
sdks/nodejs-client/src/errors/dify-error.ts
Normal 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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user