Skip to content

Commit

Permalink
fix(client): fix handling of not found errors (#219)
Browse files Browse the repository at this point in the history
* chore(server): v0.1.15

* fix(client): fix handling of not found errors

* client v0.0.7
  • Loading branch information
laurentlp committed Oct 20, 2021
1 parent 7362932 commit 025f290
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botpress/messaging-client",
"version": "0.0.6",
"version": "0.0.7",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"source": "src/index.ts",
Expand Down
25 changes: 2 additions & 23 deletions packages/client/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import axios, { AxiosInstance } from 'axios'
import { BadRequestError, UnauthorizedError, ForbiddenError, InternalServerError } from './errors'

const handleError = (err: unknown) => {
if (axios.isAxiosError(err)) {
switch (err.response?.status) {
case 400:
throw new BadRequestError(err.message)
case 401:
throw new UnauthorizedError(err.message)
case 403:
throw new ForbiddenError(err.message)
case 404:
return undefined
case 500:
throw new InternalServerError(err.message)
default:
throw err
}
}

throw err
}
import { AxiosInstance } from 'axios'
import { handleError } from './errors'

export abstract class BaseClient {
constructor(protected http: AxiosInstance) {
Expand Down
5 changes: 4 additions & 1 deletion packages/client/src/conversations.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Conversation, ConversationWithLastMessage } from '@botpress/messaging-base'
import { BaseClient } from './base'
import { handleNotFound } from './errors'

export class ConversationClient extends BaseClient {
async create(userId: string): Promise<Conversation> {
return this.deserialize((await this.http.post<Conversation>('/conversations', { userId })).data)
}

async get(id: string): Promise<Conversation | undefined> {
return this.deserialize((await this.http.get<Conversation>(`/conversations/${id}`)).data)
return handleNotFound(async () => {
return this.deserialize((await this.http.get<Conversation>(`/conversations/${id}`)).data)
}, undefined)
}

async list(userId: string, limit: number): Promise<ConversationWithLastMessage[]> {
Expand Down
39 changes: 39 additions & 0 deletions packages/client/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import axios from 'axios'

export class BadRequestError extends Error {
static statusCode = 400
}
Expand All @@ -10,6 +12,43 @@ export class ForbiddenError extends Error {
static statusCode = 403
}

export class NotFoundError extends Error {
static statusCode = 404
}

export class InternalServerError extends Error {
static statusCode = 500
}

export const handleError = (err: unknown) => {
if (axios.isAxiosError(err)) {
switch (err.response?.status) {
case 400:
throw new BadRequestError(err.message)
case 401:
throw new UnauthorizedError(err.message)
case 403:
throw new ForbiddenError(err.message)
case 404:
throw new NotFoundError(err.message)
case 500:
throw new InternalServerError(err.message)
default:
throw err
}
}

throw err
}

export const handleNotFound = async <U, T, F extends Function = () => U>(func: F, returnValue: T): Promise<U | T> => {
try {
return await func()
} catch (err) {
if (err instanceof NotFoundError) {
return returnValue
} else {
throw err
}
}
}
15 changes: 10 additions & 5 deletions packages/client/src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Message } from '@botpress/messaging-base'
import { BaseClient } from './base'
import { handleNotFound } from './errors'

export class MessageClient extends BaseClient {
async create(conversationId: string, authorId: string | undefined, payload: any): Promise<Message> {
return this.deserialize((await this.http.post<Message>('/messages', { conversationId, authorId, payload })).data)
}

async get(id: string): Promise<Message> {
return this.deserialize((await this.http.get<Message>(`/messages/${id}`)).data)
async get(id: string): Promise<Message | undefined> {
return handleNotFound(async () => {
this.deserialize((await this.http.get<Message>(`/messages/${id}`)).data)
}, undefined)
}

async list(conversationId: string, limit: number): Promise<Message[]> {
return (await this.http.get<Message[]>('/messages', { params: { conversationId, limit } })).data.map((x) =>
this.deserialize(x)
)
return handleNotFound(async () => {
return (await this.http.get<Message[]>('/messages', { params: { conversationId, limit } })).data.map((x) =>
this.deserialize(x)
)
}, [])
}

async delete(filters: { id?: string; conversationId?: string }): Promise<number> {
Expand Down
5 changes: 4 additions & 1 deletion packages/client/src/users.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { User } from '@botpress/messaging-base'
import { BaseClient } from './base'
import { handleNotFound } from './errors'

export class UserClient extends BaseClient {
async create(): Promise<User> {
return (await this.http.post<User>('/users')).data
}

async get(id: string): Promise<User | undefined> {
return (await this.http.get<User>(`/users/${id}`)).data
return handleNotFound(async () => {
return (await this.http.get<User>(`/users/${id}`)).data
}, undefined)
}
}

0 comments on commit 025f290

Please sign in to comment.