Skip to content

Commit bbf2b23

Browse files
authored
Improve handling of 404 errors from Apple developer website (#14)
1 parent 0511b74 commit bbf2b23

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/index.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { cache } from "hono/cache"
44
import { cors } from "hono/cors"
55
import { HTTPException } from "hono/http-exception"
66

7-
import { fetchJSONData } from "./lib/fetch"
7+
import { fetchJSONData, NotFoundError } from "./lib/fetch"
88
import { createMcpServer } from "./lib/mcp"
99
import { renderFromJSON } from "./lib/render"
1010
import { generateAppleDocUrl, isValidAppleDocUrl, normalizeDocumentationPath } from "./lib/url"
@@ -112,7 +112,10 @@ This service only works with Apple Developer documentation URLs:
112112
)
113113
}
114114

115-
return c.text(markdown, 200, { ...headers, "Content-Type": "text/markdown; charset=utf-8" })
115+
return c.text(markdown, 200, {
116+
...headers,
117+
"Content-Type": "text/markdown; charset=utf-8",
118+
})
116119
})
117120

118121
// Catch-all route for any other requests - returns 404
@@ -140,6 +143,42 @@ app.onError((err, c) => {
140143
return err.getResponse()
141144
}
142145

146+
if (err instanceof NotFoundError) {
147+
const accept = c.req.header("Accept")
148+
if (accept?.includes("application/json")) {
149+
return c.json(
150+
{
151+
error: "Documentation not found",
152+
message: "The requested Apple Developer documentation page does not exist.",
153+
},
154+
404,
155+
)
156+
}
157+
158+
return c.text(
159+
`# Not Found
160+
161+
The requested Apple Developer documentation page does not exist.
162+
163+
## What you can try:
164+
165+
1. **Check the URL** - Make sure the path is correct
166+
2. **Browse from a parent page** - Try starting from a higher-level documentation page
167+
3. **Search Apple Developer Documentation** - Use Apple's official search
168+
169+
## Examples of valid URLs:
170+
171+
- [Swift Documentation](https://sosumi.ai/documentation/swift)
172+
- [SwiftUI Documentation](https://sosumi.ai/documentation/swiftui)
173+
- [UIKit Documentation](https://sosumi.ai/documentation/uikit)
174+
175+
---
176+
*[sosumi.ai](https://sosumi.ai) - Making Apple docs AI-readable*`,
177+
404,
178+
{ "Content-Type": "text/markdown; charset=utf-8" },
179+
)
180+
}
181+
143182
// Handle unexpected errors
144183
const accept = c.req.header("Accept")
145184
if (accept?.includes("application/json")) {

src/lib/fetch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import type { AppleDocJSON } from "./types"
77
import { normalizeDocumentationPath } from "./url"
88

9+
export class NotFoundError extends Error {}
10+
911
/**
1012
* Fetch Apple Developer documentation JSON data for a given path
1113
*/
@@ -42,6 +44,9 @@ export async function fetchJSONData(path: string): Promise<AppleDocJSON> {
4244

4345
if (!response.ok) {
4446
console.error(`Failed to fetch JSON: ${response.status} ${response.statusText}`)
47+
if (response.status === 404) {
48+
throw new NotFoundError(`Apple documentation page not found at ${jsonUrl}`)
49+
}
4550
throw new Error(`Failed to fetch JSON: ${response.status} ${response.statusText}`)
4651
}
4752

0 commit comments

Comments
 (0)