-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: swallowed exceptions from improper exports
Adapters were causing multiple instances of OAuthExceptions to be created, one for each instance. Instead of going down the bundler rabbit hole, I was thinking about how to fix this, and I realized that the simplest way is to add a marker on the OAuthException (OAuthException.oauth) and check for it instead of using instance of. Resolves #82 #82 (comment)
- Loading branch information
1 parent
580922f
commit 9ff4e7c
Showing
6 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { OAuthException } from "../exceptions/oauth.exception.js"; | ||
|
||
export function isOAuthError(error: unknown): error is OAuthException { | ||
if (!error) return false; | ||
if (typeof error !== "object") return false; | ||
return "oauth" in error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { describe, it, expect, vi } from "vitest"; | ||
import { FastifyReply, FastifyRequest } from "fastify"; | ||
import { handleFastifyError, requestFromFastify, responseFromFastify } from "../../../src/adapters/fastify.js"; | ||
import { ErrorType, OAuthException, OAuthRequest, OAuthResponse } from "../../../src/index.js"; | ||
|
||
describe("Fastify OAuth Utilities", () => { | ||
describe("responseFromFastify", () => { | ||
it("should create an OAuthResponse from FastifyReply", () => { | ||
const mockReply = { | ||
headers: { "Content-Type": "application/json" }, | ||
} as unknown as FastifyReply; | ||
|
||
const result = responseFromFastify(mockReply); | ||
expect(result).toBeInstanceOf(OAuthResponse); | ||
expect(result.headers).toEqual({ "Content-Type": "application/json" }); | ||
}); | ||
}); | ||
|
||
describe("requestFromFastify", () => { | ||
it("should create an OAuthRequest from FastifyRequest", () => { | ||
const mockRequest = { | ||
query: { code: "abc123" }, | ||
body: { grant_type: "authorization_code" }, | ||
headers: { "Content-Type": "application/json" }, | ||
} as unknown as FastifyRequest; | ||
|
||
const result = requestFromFastify(mockRequest); | ||
expect(result).toBeInstanceOf(OAuthRequest); | ||
expect(result.query).toEqual({ code: "abc123" }); | ||
expect(result.body).toEqual({ grant_type: "authorization_code" }); | ||
expect(result.headers).toEqual({ "Content-Type": "application/json" }); | ||
}); | ||
}); | ||
|
||
describe("handleFastifyError", () => { | ||
it("should handle OAuthException", () => { | ||
const mockReply = { | ||
status: vi.fn().mockReturnThis(), | ||
send: vi.fn(), | ||
} as unknown as FastifyReply; | ||
|
||
const oauthError = new OAuthException("Test error", ErrorType.InternalServerError); | ||
handleFastifyError(oauthError, mockReply); | ||
|
||
expect(mockReply.status).toHaveBeenCalledWith(400); | ||
expect(mockReply.send).toHaveBeenCalledWith({ | ||
status: 400, | ||
message: "Test error", | ||
}); | ||
}); | ||
|
||
it("should throw non-OAuthException errors", () => { | ||
const mockReply = {} as FastifyReply; | ||
const error = new Error("Regular error"); | ||
|
||
expect(() => handleFastifyError(error, mockReply)).toThrow("Regular error"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters