Skip to content

Commit

Permalink
Ensure that additional middleware doesn't prevent proper error handli…
Browse files Browse the repository at this point in the history
…ng (#1559)

* Ensure that middleware does prevent proper error handling

* Update tests
  • Loading branch information
NolanTrem authored Nov 6, 2024
1 parent 4c0f449 commit 1467be8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 39 deletions.
45 changes: 23 additions & 22 deletions js/sdk/__tests__/r2rClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,39 @@ describe("R2RClient", () => {
const mockResponse = { success: true };
mockAxiosInstance.request.mockResolvedValue({ data: mockResponse });

const email = "[email protected]";
const verification_code = "123456";
const result = await client.verifyEmail(verification_code);
const result = await client.verifyEmail(email, verification_code);

expect(result).toEqual(mockResponse);
expect(mockAxiosInstance.request).toHaveBeenCalledWith({
method: "POST",
url: "verify_email",
data: JSON.stringify({ verification_code }),
data: JSON.stringify({ email, verification_code }),
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
});

test("requestPasswordReset should send POST request to /request_password_reset with correct data", async () => {
const mockResponse = { success: true };
mockAxiosInstance.request.mockResolvedValue({ data: mockResponse });

const email = "[email protected]";
const result = await client.requestPasswordReset(email);

expect(result).toEqual(mockResponse);
expect(mockAxiosInstance.request).toHaveBeenCalledWith({
method: "POST",
url: "request_password_reset",
data: '"[email protected]"',
headers: {
"Content-Type": "application/json",
},
responseType: "json",
params: undefined,
});
});

Expand Down Expand Up @@ -264,26 +285,6 @@ describe("R2RClient", () => {
});
});

test("requestPasswordReset should send POST request to /request_password_reset with correct data", async () => {
const mockResponse = { success: true };
mockAxiosInstance.request.mockResolvedValue({ data: mockResponse });

const email = "[email protected]";

const result = await client.requestPasswordReset(email);

expect(result).toEqual(mockResponse);
expect(mockAxiosInstance.request).toHaveBeenCalledWith({
method: "POST",
url: "request_password_reset",
data: JSON.stringify({ email }),
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
});

test("confirmPasswordReset should send POST request to /reset_password/{resetToken} with correct data", async () => {
const mockResponse = { success: true };
mockAxiosInstance.request.mockResolvedValue({ data: mockResponse });
Expand Down
8 changes: 7 additions & 1 deletion js/sdk/__tests__/r2rClientIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let newCollectionId: string;
* X updateUser
* - refreshAccessToken
* X changePassword
* X requestPasswordReset
* - requestPasswordReset
* X confirmPasswordReset
* X deleteUser
* Ingestion:
Expand Down Expand Up @@ -241,6 +241,12 @@ describe("r2rClient Integration Tests", () => {
await expect(client.refreshAccessToken()).resolves.not.toThrow();
});

test("Request password reset", async () => {
await expect(
client.requestPasswordReset("[email protected]"),
).resolves.not.toThrow();
});

test("Get analytics", async () => {
const filterCriteria: Record<string, any> | string = {
search_latencies: "search_latency",
Expand Down
8 changes: 7 additions & 1 deletion js/sdk/__tests__/r2rClientIntegrationUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const baseUrl = "http://localhost:7272";
* - updateUser
* - refreshAccessToken
* - changePassword
* X requestPasswordReset
* - requestPasswordReset
* X confirmPasswordReset
* - deleteUser
* Ingestion:
Expand Down Expand Up @@ -283,6 +283,12 @@ describe("r2rClient Integration Tests", () => {
).resolves.not.toThrow();
});

test("Request password reset", async () => {
await expect(
client.requestPasswordReset("[email protected]"),
).resolves.not.toThrow();
});

test("Delete User", async () => {
const currentUser = await client.user();
await expect(
Expand Down
2 changes: 1 addition & 1 deletion js/sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "r2r-js",
"version": "0.3.14",
"version": "0.3.15",
"description": "",
"main": "dist/index.js",
"browser": "dist/index.browser.js",
Expand Down
29 changes: 19 additions & 10 deletions js/sdk/src/r2rClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ export class r2rClient {
* @returns A promise that resolves to the response from the server.
*/
@feature("verifyEmail")
async verifyEmail(verification_code: string): Promise<any> {
async verifyEmail(email: string, verification_code: string): Promise<any> {
return await this._makeRequest("POST", "verify_email", {
data: { verification_code },
data: { email, verification_code },
});
}

Expand Down Expand Up @@ -427,8 +427,11 @@ export class r2rClient {
*/
@feature("requestPasswordReset")
async requestPasswordReset(email: string): Promise<any> {
return this._makeRequest("POST", "request_password_reset", {
data: { email },
return await this._makeRequest("POST", "request_password_reset", {
data: JSON.stringify(email),
headers: {
"Content-Type": "application/json",
},
});
}

Expand Down Expand Up @@ -687,12 +690,16 @@ export class r2rClient {
@feature("updateDocumentMetadata")
async updateDocumentMetadata(
documentId: string,
metadata: Record<string, any>
metadata: Record<string, any>,
): Promise<Record<string, any>> {
this._ensureAuthenticated();
return await this._makeRequest("POST", `update_document_metadata/${documentId}`, {
data: metadata,
});
return await this._makeRequest(
"POST",
`update_document_metadata/${documentId}`,
{
data: metadata,
},
);
}

@feature("ingestChunks")
Expand Down Expand Up @@ -1911,7 +1918,7 @@ export class r2rClient {
//
// -----------------------------------------------------------------------------

/**
/**
* Search over documents.
* @param query The query to search for.
* @param settings Settings for the document search.
Expand Down Expand Up @@ -1949,7 +1956,9 @@ export class r2rClient {
},
};

return await this._makeRequest("POST", "search_documents", { data: json_data });
return await this._makeRequest("POST", "search_documents", {
data: json_data,
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions py/core/main/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Union

from core.base import R2RException
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import JSONResponse

from core.base import R2RException
from core.providers import (
HatchetOrchestrationProvider,
SimpleOrchestrationProvider,
Expand Down
16 changes: 15 additions & 1 deletion py/core/main/app_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from typing import Optional

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from fastapi import FastAPI
from core.base import R2RException
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware

from .assembly import R2RBuilder, R2RConfig
Expand Down Expand Up @@ -117,6 +119,18 @@ async def create_r2r_app(
# Create the FastAPI app
app = FastAPI(lifespan=lifespan)


@app.exception_handler(R2RException)
async def r2r_exception_handler(request: Request, exc: R2RException):
return JSONResponse(
status_code=exc.status_code,
content={
"message": exc.message,
"error_type": type(exc).__name__,
},
)


# Add CORS middleware
app.add_middleware(
CORSMiddleware,
Expand Down

0 comments on commit 1467be8

Please sign in to comment.