-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: propagate a 429 status code if email-provider rate limiting occurs
- Loading branch information
1 parent
4377ba7
commit 5c3260e
Showing
3 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { flattenErrors, statusCodeForError } from "lib/graphqlErrorHandler" | ||
|
||
// Log in `extensions` if this request was by an email provider and resulted | ||
// in a 429 rate limit error from a downstream request made using `fetch`. | ||
export const emailProviderRateLimitExtension = (_documentAST, result) => { | ||
const extensions = {} | ||
|
||
if (result.errors && result.errors.length) { | ||
result.errors.some((err) => { | ||
flattenErrors(err).some((e) => { | ||
const httpStatusCode = statusCodeForError(e) | ||
if (httpStatusCode === 429) { | ||
extensions["emailProviderLimited"] = true | ||
return true | ||
} | ||
return false | ||
}) | ||
}) | ||
} | ||
|
||
return extensions | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/integration/__tests__/email_provider_rate_limit.test.ts
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,37 @@ | ||
jest.mock("lib/apis/fetch", () => jest.fn()) | ||
import { HTTPError } from "lib/HTTPError" | ||
import fetch from "lib/apis/fetch" | ||
const mockFetch = fetch as jest.Mock | ||
|
||
describe("rate limiting custom status code and response", () => { | ||
const request = require("supertest") | ||
const app = require("../../index").default | ||
const gql = require("lib/gql").default | ||
|
||
beforeEach(() => { | ||
mockFetch.mockClear() | ||
mockFetch.mockReset() | ||
}) | ||
|
||
it("propagates a 429 for email provider rate limiting", async () => { | ||
mockFetch.mockRejectedValueOnce( | ||
new HTTPError("Braze rate limit reached", 429) | ||
) | ||
|
||
const response = await request(app) | ||
.post("/v2") | ||
.set("Accept", "application/json") | ||
.send({ | ||
query: gql` | ||
{ | ||
artist(id: "banksy") { | ||
name | ||
} | ||
} | ||
`, | ||
}) | ||
|
||
expect(response.statusCode).toBe(429) | ||
expect(response.body.error).toBe("rate limit reached, try again later") | ||
}) | ||
}) |