-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updates to static handler for Remix integration #9511
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
074d641
Updates to unstable staticHandler logic for Remix integration
brophdawg11 8eee81c
Add changeset
brophdawg11 80fdd5a
bump bundle
brophdawg11 d9c4dce
Remove ErrorWithStatus in favor of extended ErrorResponse
brophdawg11 055dfe4
update
brophdawg11 ce34caa
Merge branch 'dev' into brophdawg11/rrr-updates
brophdawg11 51f453a
Update changelog
brophdawg11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 @@ | ||
--- | ||
"@remix-run/router": patch | ||
--- | ||
|
||
fix: actions/loaders returning undefined should throw an 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
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 |
---|---|---|
|
@@ -32,7 +32,11 @@ import type { | |
AgnosticRouteObject, | ||
TrackedPromise, | ||
} from "../utils"; | ||
import { AbortedDeferredError, stripBasename } from "../utils"; | ||
import { | ||
AbortedDeferredError, | ||
isErrorWithStatus, | ||
stripBasename, | ||
} from "../utils"; | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
//#region Types and Utils | ||
|
@@ -5069,6 +5073,61 @@ describe("a router", () => { | |
|
||
router.dispose(); | ||
}); | ||
|
||
it("throws an error if actions/loaders return undefined", async () => { | ||
let t = setup({ | ||
routes: [ | ||
{ | ||
index: true, | ||
}, | ||
{ | ||
id: "path", | ||
path: "/path", | ||
loader: true, | ||
action: true, | ||
}, | ||
], | ||
}); | ||
|
||
let nav1 = await t.navigate("/path"); | ||
await nav1.loaders.path.resolve(undefined); | ||
expect(t.router.state).toMatchObject({ | ||
location: { | ||
pathname: "/path", | ||
}, | ||
errors: { | ||
path: new Error( | ||
'You defined a loader for route "path" but didn\'t return anything ' + | ||
"from your `loader` function. Please return a value or `null`." | ||
), | ||
}, | ||
}); | ||
|
||
await t.navigate("/"); | ||
expect(t.router.state).toMatchObject({ | ||
location: { | ||
pathname: "/", | ||
}, | ||
errors: {}, | ||
}); | ||
|
||
let nav3 = await t.navigate("/path", { | ||
formMethod: "post", | ||
formData: createFormData({}), | ||
}); | ||
await nav3.actions.path.resolve(undefined); | ||
expect(t.router.state).toMatchObject({ | ||
location: { | ||
pathname: "/path", | ||
}, | ||
errors: { | ||
path: new Error( | ||
'You defined an action for route "path" but didn\'t return anything ' + | ||
"from your `action` function. Please return a value or `null`." | ||
), | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("redirects", () => { | ||
|
@@ -11066,6 +11125,35 @@ describe("a router", () => { | |
expect(data).toBe(""); | ||
}); | ||
|
||
it("should error if an action/loader returns undefined", async () => { | ||
let T = setupFlexRouteTest(); | ||
let data; | ||
|
||
try { | ||
data = await T.resolveLoader(undefined); | ||
} catch (e) { | ||
data = e; | ||
} | ||
expect(data).toEqual( | ||
new Error( | ||
'You defined a loader for route "flex" but didn\'t return anything ' + | ||
"from your `loader` function. Please return a value or `null`." | ||
) | ||
); | ||
|
||
try { | ||
data = await T.resolveAction(undefined); | ||
} catch (e) { | ||
data = e; | ||
} | ||
expect(data).toEqual( | ||
new Error( | ||
'You defined an action for route "flex" but didn\'t return anything ' + | ||
"from your `action` function. Please return a value or `null`." | ||
) | ||
); | ||
}); | ||
|
||
it("should handle relative redirect responses (loader)", async () => { | ||
let { queryRoute } = createStaticHandler([ | ||
{ | ||
|
@@ -11214,7 +11302,7 @@ describe("a router", () => { | |
); | ||
}); | ||
|
||
it("should handle not found routes with a 404 Response", async () => { | ||
describe("Errors with Status Codes", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. View this section with whitespace hidden, but now instead of returning responses we return a new |
||
/* eslint-disable jest/no-conditional-expect */ | ||
let { queryRoute } = createStaticHandler([ | ||
{ | ||
|
@@ -11223,93 +11311,83 @@ describe("a router", () => { | |
}, | ||
]); | ||
|
||
try { | ||
await queryRoute(createRequest("/junk")); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(data instanceof Response).toBe(true); | ||
expect(data.status).toBe(404); | ||
expect(data.statusText).toBe("Not Found"); | ||
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes"); | ||
} | ||
|
||
try { | ||
await queryRoute(createRequest("/"), "junk"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(data instanceof Response).toBe(true); | ||
expect(data.status).toBe(404); | ||
expect(data.statusText).toBe("Not Found"); | ||
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes"); | ||
} | ||
it("should handle not found paths with a 404 Response", async () => { | ||
try { | ||
await queryRoute(createRequest("/junk")); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(isErrorWithStatus(data)).toBe(true); | ||
expect(data.status).toBe(404); | ||
expect(data.message).toBe('No route matches URL "/junk"'); | ||
} | ||
|
||
try { | ||
await queryRoute(createSubmitRequest("/junk")); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(data instanceof Response).toBe(true); | ||
expect(data.status).toBe(404); | ||
expect(data.statusText).toBe("Not Found"); | ||
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes"); | ||
} | ||
try { | ||
await queryRoute(createSubmitRequest("/junk")); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(isErrorWithStatus(data)).toBe(true); | ||
expect(data.status).toBe(404); | ||
expect(data.message).toBe('No route matches URL "/junk"'); | ||
} | ||
}); | ||
|
||
try { | ||
await queryRoute(createSubmitRequest("/"), "junk"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(data instanceof Response).toBe(true); | ||
expect(data.status).toBe(404); | ||
expect(data.statusText).toBe("Not Found"); | ||
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes"); | ||
} | ||
it("should handle not found routeIds with a 403 Response", async () => { | ||
try { | ||
await queryRoute(createRequest("/"), "junk"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(isErrorWithStatus(data)).toBe(true); | ||
expect(data.status).toBe(403); | ||
expect(data.message).toBe('Route "junk" does not match URL "/"'); | ||
} | ||
|
||
/* eslint-enable jest/no-conditional-expect */ | ||
}); | ||
try { | ||
await queryRoute(createSubmitRequest("/"), "junk"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(isErrorWithStatus(data)).toBe(true); | ||
expect(data.status).toBe(403); | ||
expect(data.message).toBe('Route "junk" does not match URL "/"'); | ||
} | ||
}); | ||
|
||
it("should handle not found action submissions with a 405 Response", async () => { | ||
/* eslint-disable jest/no-conditional-expect */ | ||
let { queryRoute } = createStaticHandler([ | ||
{ | ||
id: "root", | ||
path: "/", | ||
}, | ||
]); | ||
it("should handle not found action/loader submissions with a 405 Response", async () => { | ||
try { | ||
await queryRoute(createRequest("/"), "root"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(isErrorWithStatus(data)).toBe(true); | ||
expect(data.status).toBe(405); | ||
expect(data.message).toBe( | ||
'You made a GET request to "/" but did not provide a `loader` ' + | ||
'for route "root", so there is no way to handle the request.' | ||
); | ||
} | ||
|
||
try { | ||
await queryRoute(createSubmitRequest("/"), "root"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(data instanceof Response).toBe(true); | ||
expect(data.status).toBe(405); | ||
expect(data.statusText).toBe("Method Not Allowed"); | ||
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes"); | ||
expect(await data.text()).toBe(""); | ||
} | ||
/* eslint-enable jest/no-conditional-expect */ | ||
}); | ||
try { | ||
await queryRoute(createSubmitRequest("/"), "root"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(isErrorWithStatus(data)).toBe(true); | ||
expect(data.status).toBe(405); | ||
expect(data.message).toBe( | ||
'You made a POST request to "/" but did not provide an `action` ' + | ||
'for route "root", so there is no way to handle the request.' | ||
); | ||
} | ||
}); | ||
|
||
it("should handle unsupported methods with a 405 Response", async () => { | ||
/* eslint-disable jest/no-conditional-expect */ | ||
let { queryRoute } = createStaticHandler([ | ||
{ | ||
id: "root", | ||
path: "/", | ||
}, | ||
]); | ||
it("should handle unsupported methods with a 405 Response", async () => { | ||
try { | ||
await queryRoute(createRequest("/", { method: "OPTIONS" }), "root"); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(isErrorWithStatus(data)).toBe(true); | ||
expect(data.status).toBe(405); | ||
expect(data.message).toBe('Invalid request method "OPTIONS"'); | ||
} | ||
}); | ||
|
||
try { | ||
await queryRoute( | ||
createSubmitRequest("/", { method: "OPTIONS" }), | ||
"root" | ||
); | ||
expect(false).toBe(true); | ||
} catch (data) { | ||
expect(data instanceof Response).toBe(true); | ||
expect(data.status).toBe(405); | ||
expect(data.statusText).toBe("Method Not Allowed"); | ||
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes"); | ||
expect(await data.text()).toBe(""); | ||
} | ||
/* eslint-enable jest/no-conditional-expect */ | ||
}); | ||
}); | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests used to return
undefined
, so make them returnnull
now