Skip to content

Commit 2b57ce8

Browse files
committed
feat: add ErrorUserNull
1 parent 7cd0c20 commit 2b57ce8

10 files changed

Lines changed: 55 additions & 12 deletions

src/errors/AskCodebaseError.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
import { JsonResp } from "../typings";
12
import { AskCodebaseErrorCode } from "./AskCodebaseErrorCode";
23

34
export class AskCodebaseError extends Error {
4-
public code: AskCodebaseErrorCode = AskCodebaseErrorCode.E1000;
5+
public code: AskCodebaseErrorCode = AskCodebaseErrorCode.E10001;
56

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

11-
public getCode() {
12-
return this.code
12+
public serialize(): JsonResp<null> {
13+
return {
14+
data: null,
15+
error: this.message,
16+
errcode: this.code
17+
}
1318
}
1419

1520
public toString() {

src/errors/AskCodebaseErrorCode.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export enum AskCodebaseErrorCode {
2+
/** Success */
3+
E0000 = 0,
24
/** Unkonwn error */
3-
E1000 = 'E1000',
5+
E10001 = 10001,
46
/** Access token expired */
5-
E1001 = 'E1001',
7+
E10002 = 10002,
68
/** API usage limit exceeded */
7-
E1002 = 'E1002',
9+
E10003 = 10003,
10+
/** User is null */
11+
E10004 = 10004,
812
}

src/errors/ErrorApiUsageExceeded.ts

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

44
export class ErrorApiUsageExceeded extends AskCodebaseError {
5+
public static code = AskCodebaseErrorCode.E10003
6+
57
constructor(message: string) {
6-
super(AskCodebaseErrorCode.E1002, message);
8+
super(ErrorApiUsageExceeded.code, message);
79
}
810
}

src/errors/ErrorTokenExpired.ts

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

44
export class ErrorTokenExpired extends AskCodebaseError {
5+
public static code = AskCodebaseErrorCode.E10002
6+
57
constructor(message: string) {
6-
super(AskCodebaseErrorCode.E1001, message);
8+
super(ErrorTokenExpired.code, message);
79
}
810
}

src/errors/ErrorUserNull.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { AskCodebaseError } from "./AskCodebaseError";
2+
import { AskCodebaseErrorCode } from "./AskCodebaseErrorCode";
3+
4+
export class ErrorUserNull extends AskCodebaseError {
5+
public static code = AskCodebaseErrorCode.E10004
6+
7+
constructor(message: string) {
8+
super(ErrorUserNull.code, message);
9+
}
10+
}

src/errors/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './AskCodebaseErrorCode'
22
export * from './AskCodebaseError'
33
export * from './ErrorTokenExpired'
4-
export * from './ErrorApiUsageExceeded'
4+
export * from './ErrorApiUsageExceeded'
5+
export * from './ErrorUserNull'

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './errors'
1+
export * from './errors'
2+
export * from './typings'

src/typings/IUser.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface IUser {
2+
userID: string
3+
email: string
4+
username: string
5+
displayName: string
6+
avatar: string
7+
jwt: string
8+
apiUsage: number
9+
apiLimit: number
10+
subscriptionType: string
11+
subscriptionStatus: string
12+
subscriptionEndDate: number
13+
}

src/typings/JsonResp.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { AskCodebaseErrorCode } from "../errors";
2+
3+
export type JsonResp<T> = { data: T, error: string, errcode: AskCodebaseErrorCode }

src/typings/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './IUser'
2+
export * from './JsonResp'

0 commit comments

Comments
 (0)