Skip to content

Commit d2ad260

Browse files
committed
feat: add error none
1 parent 200961d commit d2ad260

4 files changed

Lines changed: 46 additions & 22 deletions

File tree

src/errors/AskCodebaseError.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import { JsonResp } from "../typings";
22
import { AskCodebaseErrorCode } from "./AskCodebaseErrorCode";
33

44
export class AskCodebaseError extends Error {
5-
public code: AskCodebaseErrorCode = AskCodebaseErrorCode.E10001;
5+
public code: AskCodebaseErrorCode = AskCodebaseErrorCode.E10001;
66

7-
constructor(code: AskCodebaseErrorCode, message: string) {
8-
super(message)
9-
this.code = code
10-
}
7+
constructor(code: AskCodebaseErrorCode, message: string) {
8+
super(message);
9+
this.code = code;
10+
}
1111

12-
public serialize(): JsonResp<null> {
13-
return {
14-
data: null,
15-
error: this.message,
16-
errcode: this.code
17-
}
18-
}
12+
public serialize(): JsonResp<any> {
13+
return {
14+
data: null,
15+
error: this.message,
16+
errcode: this.code,
17+
};
18+
}
1919

20-
public toString() {
21-
return `${this.code}: ${this.message}`;
22-
}
23-
}
20+
public toString() {
21+
return `${this.code}: ${this.message}`;
22+
}
23+
}

src/errors/ErrorNone.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { JsonResp } from "../typings";
2+
import { AskCodebaseError } from "./AskCodebaseError";
3+
import { AskCodebaseErrorCode } from "./AskCodebaseErrorCode";
4+
5+
export class ErrorNone<T> extends AskCodebaseError {
6+
public static code = AskCodebaseErrorCode.E0000;
7+
8+
constructor(public data: T, message: string = "Success") {
9+
super(ErrorNone.code, message);
10+
}
11+
12+
public serialize(): JsonResp<T> {
13+
return {
14+
data: this.data,
15+
error: null,
16+
errcode: this.code,
17+
};
18+
}
19+
}

src/errors/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export * from './AskCodebaseErrorCode'
2-
export * from './AskCodebaseError'
3-
export * from './ErrorTokenExpired'
4-
export * from './ErrorApiUsageExceeded'
5-
export * from './ErrorUserNull'
1+
export * from "./AskCodebaseErrorCode";
2+
export * from "./AskCodebaseError";
3+
export * from "./ErrorTokenExpired";
4+
export * from "./ErrorApiUsageExceeded";
5+
export * from "./ErrorUserNull";
6+
export * from "./ErrorNone";

src/typings/JsonResp.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { AskCodebaseErrorCode } from "../errors";
22

3-
export type JsonResp<T> = { data: T, error: string, errcode: AskCodebaseErrorCode }
3+
export type JsonResp<T> = {
4+
data: T;
5+
error: string | null;
6+
errcode: AskCodebaseErrorCode;
7+
};

0 commit comments

Comments
 (0)