Skip to content

Commit 4677aa8

Browse files
committed
improve error responses
1 parent 4835b25 commit 4677aa8

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

Sources/UnidocAssets/Unidoc.Asset.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ extension Unidoc
1010
case favicon_ico = "favicon.ico"
1111
case favicon_png = "favicon.png"
1212

13+
case github_jpg = "github.jpg"
14+
1315
// We let Google Fonts serve most of the fonts, but we host Literata ourselves
1416
// because the front-end CSS uses opentype features such as old-style numerals,
1517
// and Google Fonts strips those out.
@@ -62,6 +64,7 @@ extension Unidoc.Asset
6264
case .error500_jpg: false
6365
case .favicon_ico: false
6466
case .favicon_png: false
67+
case .github_jpg: false
6568
case .literata45_woff2: false
6669
case .literata47_woff2: false
6770
case .literata75_woff2: false

Sources/UnidocAssets_System/Unidoc.Asset (ext).swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extension Unidoc.Asset
1414
case .error500_jpg: ["icons", "error500.jpg"]
1515
case .favicon_ico: ["icons", "favicon.ico"]
1616
case .favicon_png: ["icons", "favicon.png"]
17+
case .github_jpg: ["icons", "github.jpg"]
1718
case .literata45_woff2: ["woff2", "Literata_24pt-Regular.woff2"]
1819
case .literata47_woff2: ["woff2", "Literata_24pt-Italic.woff2"]
1920
case .literata75_woff2: ["woff2", "Literata_24pt-Bold.woff2"]
@@ -37,6 +38,7 @@ extension Unidoc.Asset
3738
case .error500_jpg: .image(.jpeg)
3839
case .favicon_ico: .image(.x_icon)
3940
case .favicon_png: .image(.png)
41+
case .github_jpg: .image(.jpeg)
4042
case .literata45_woff2: .font(.woff2)
4143
case .literata47_woff2: .font(.woff2)
4244
case .literata75_woff2: .font(.woff2)

Sources/UnidocServer/Operations/Interactions/Unidoc.PackageIndexOperation.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension Unidoc.PackageIndexOperation:Unidoc.MeteredOperation
4545

4646
switch self.subject
4747
{
48-
case .repo(owner: let owner, name: let repo, githubInstallation: let appInstallation):
48+
case .repo(owner: let owner, name: let name, githubInstallation: let appInstallation):
4949
if let error:Unidoc.PolicyErrorPage = try await self.charge(cost: 8,
5050
from: server,
5151
with: session)
@@ -56,15 +56,19 @@ extension Unidoc.PackageIndexOperation:Unidoc.MeteredOperation
5656
let repo:GitHub.Repo? = try await github.connect(
5757
with: .init(githubInstallation: appInstallation))
5858
{
59-
try await $0.lookup(owner: owner, repo: repo)
59+
try await $0.lookup(owner: owner, repo: name)
6060
}
6161

6262
guard
6363
let repo:GitHub.Repo
6464
else
6565
{
66-
let display:Unidoc.PolicyErrorPage = .init(illustration: .error404_jpg,
67-
message: "No such GitHub repository!",
66+
let display:Unidoc.PolicyErrorPage = .init(illustration: .github_jpg,
67+
heading: "No such GitHub repository!",
68+
message: """
69+
The role you selected does not have access to a GitHub repository with the \
70+
name '\(owner)/\(name)'.
71+
""",
6872
status: 404)
6973
return display.response(format: format)
7074
}
@@ -89,7 +93,7 @@ extension Unidoc.PackageIndexOperation:Unidoc.MeteredOperation
8993
with: session)
9094
}
9195

92-
case .ref(let id, ref: let ref):
96+
case .ref(let id, ref: let name):
9397
if let metadata:Unidoc.PackageMetadata = try await server.db.packages.find(id: id,
9498
with: session)
9599
{
@@ -116,14 +120,21 @@ extension Unidoc.PackageIndexOperation:Unidoc.MeteredOperation
116120

117121
let ref:GitHub.Ref? = try await github.connect(with: .init())
118122
{
119-
try await $0.lookup(owner: origin.owner, repo: origin.name, ref: ref)
123+
try await $0.lookup(owner: origin.owner, repo: origin.name, ref: name)
120124
}
121125

122126
guard
123127
let ref:GitHub.Ref
124128
else
125129
{
126-
return .notFound("No such ref")
130+
let display:Unidoc.PolicyErrorPage = .init(illustration: .github_jpg,
131+
heading: "No such ref!",
132+
message: """
133+
Could not find the ref '\(name)' in the GitHub repository \
134+
'\(origin.owner)/\(origin.name)'.
135+
""",
136+
status: 404)
137+
return display.response(format: format)
127138
}
128139

129140
let version:SemanticVersion? = package.symbol.version(tag: ref.name)
@@ -174,6 +185,7 @@ extension Unidoc.PackageIndexOperation
174185
if rights < .editor
175186
{
176187
return .init(illustration: .error4xx_jpg,
188+
heading: "Insufficient permissions",
177189
message: "You are not authorized to index this repository!",
178190
status: 403)
179191
}
@@ -186,6 +198,7 @@ extension Unidoc.PackageIndexOperation
186198
else
187199
{
188200
return .init(illustration: .error4xx_jpg,
201+
heading: "Server policy error",
189202
message: "Cannot index a repository with a dot in the owner’s name!",
190203
status: 400)
191204
}

Sources/UnidocServer/Operations/Interactive/Unidoc.MeteredOperation.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ extension Unidoc.MeteredOperation
4848
else
4949
{
5050
return .init(illustration: .error4xx_jpg,
51-
message: "Inactive or nonexistent API key",
51+
heading: "Inactive or nonexistent API key",
52+
message: """
53+
The key may have been changed, or it may have exceeded its hourly rate limit.
54+
""",
5255
status: 429)
5356
}
5457
}

Sources/UnidocServer/Server/Unidoc.PolicyErrorPage.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ extension Unidoc
66
struct PolicyErrorPage
77
{
88
let illustration:Unidoc.Asset
9+
let heading:String
910
let message:String
1011
let status:UInt
1112

12-
init(illustration:Unidoc.Asset, message:String, status:UInt)
13+
init(illustration:Unidoc.Asset, heading:String, message:String, status:UInt)
1314
{
1415
self.illustration = illustration
16+
self.heading = heading
1517
self.message = message
1618
self.status = status
1719
}
@@ -22,14 +24,15 @@ extension Unidoc.PolicyErrorPage:Unidoc.StatusBearingPage
2224
}
2325
extension Unidoc.PolicyErrorPage:Unidoc.RenderablePage, Unidoc.DynamicPage
2426
{
25-
var title:String { "Policy error" }
27+
var title:String { self.heading }
2628
}
2729
extension Unidoc.PolicyErrorPage:Unidoc.ApplicationPage
2830
{
2931
func main(_ main:inout HTML.ContentEncoder, format:Unidoc.RenderFormat)
3032
{
3133
main[.section]
3234
{
35+
$0[.h1] = self.heading
3336
$0[.p] = self.message
3437
$0[.img]
3538
{

0 commit comments

Comments
 (0)