Skip to content

Commit 2192184

Browse files
committed
feat: add error response type
1 parent 6d92a94 commit 2192184

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

src/controllers/anthropicController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function handleMessage(req: Request, res: Response){
6161
const response = createMessage(request);
6262

6363
//Check if it's an error response
64-
if ('error' in response) {
64+
if (response.type === 'error') {
6565
return res.status(400).json(response);
6666
}
6767

src/services/anthropicService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import{ Model, ModelsResponse, MessagesRequest, MessagesResponse, Usage, MessageStartEvent, MessageDeltaEvent, MessageStopEvent,ContentBlockStartEvent, ContentBlockDeltaEvent} from '../types/anthropic';
1+
import{ Model, ModelsResponse, MessagesRequest, MessagesResponse, Usage, MessageStartEvent, MessageDeltaEvent, MessageStopEvent,ContentBlockStartEvent, ContentBlockDeltaEvent, ErrorResponse} from '../types/anthropic';
22

33
import { anthropicMockModels } from '../data/anthropicMockData';
44
import { getCurrentTimestamp, formatErrorResponse, calculateTokens, generateMessageId, findModelById, SSEMessageFormatter} from '../utils/anthropicHelpers';
@@ -24,7 +24,7 @@ export function getModels(): ModelsResponse {
2424
/**
2525
* Non-streaming response
2626
*/
27-
export function createMessage(request: MessagesRequest): MessagesResponse{
27+
export function createMessage(request: MessagesRequest): MessagesResponse | ErrorResponse{
2828
//Validate Model
2929
const model = findModelById(request.model);
3030
if(!model){

src/types/anthropic.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ export interface MessagesResponse{
109109
container: Container | null;
110110
}
111111

112+
export interface ErrorResponse{
113+
error: {
114+
message: string,
115+
type: string,
116+
},
117+
type: "error"
118+
}
119+
112120
export interface MessageStartEvent{
113121
type: 'message_start';
114122
message: MessagesResponse;

src/utils/anthropicHelpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MockModel } from "../types/index";
22
import { anthropicMockModels } from "../data/anthropicMockData";
3-
import { StreamingEvent } from "../types/anthropic";
3+
import { ErrorResponse, StreamingEvent } from "../types/anthropic";
44

55
/**
66
* Get current timestamp
@@ -33,7 +33,7 @@ export function findModelById(modelId: string): MockModel | undefined {
3333
/**
3434
* Format error response
3535
*/
36-
export function formatErrorResponse(message: string): any {
36+
export function formatErrorResponse(message: string): ErrorResponse {
3737
return {
3838
error: {
3939
message: message,

0 commit comments

Comments
 (0)