Skip to content

Commit 701d313

Browse files
committed
add constraints to url mappings and complete the 500 and 404 pages
1 parent e2a28f5 commit 701d313

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

grails-app/conf/UrlMappings.groovy

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ class UrlMappings {
1212
"/robots.txt" (view: "/robots")
1313
"/about"(view: "/about")
1414
"/api"(controller: 'api', action: 'index')
15-
"/$id?"(controller: 'blob', action: 'load')
15+
"/$id?" {
16+
controller = 'blob'
17+
action = 'load'
18+
constraints {
19+
id(matches: /[0-9a-f]{24}/)
20+
21+
}
22+
}
1623
"500"(view:'/error')
24+
"404"(view:'/notfound')
1725
}
1826
}

grails-app/controllers/jsonblob/BlobController.groovy

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ package jsonblob
33
class BlobController {
44

55
def jsonBlobResourceService
6-
def jsonService
76

87
def load() {
98
def jsonBlobId = params.get("id")
109
try {
11-
def blob = jsonBlobResourceService.read(jsonBlobId);
12-
def id = blob["_id"]
13-
blob.remove("_id")
14-
def json = jsonService.objectMapper.writeValueAsString(blob)
15-
render(view: '../editor', model: [blob: json, blobId: id])
16-
} catch (Exception exception) {
17-
log.error("Couldn't load object with id $jsonBlobId", exception)
18-
redirect(uri: "/")
10+
def blob = jsonBlobResourceService.read(jsonBlobId);
11+
if (blob) {
12+
def id = blob["_id"]
13+
render(view: '../editor', model: [blobId: id])
14+
} else {
15+
response.status = 404
16+
}
17+
} catch (Exception e) {
18+
log.error("Couldn't load blob with id=$jsonBlobId")
19+
response.status = 404
1920
}
2021
}
2122

grails-app/views/error.gsp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
<html>
33
<head>
44
<meta name="layout" content="bootstrap"/>
5-
<title>About JSON Blob</title>
5+
<title>Something Broke</title>
66
</head>
77

88
<body>
99
<div class="row-fluid">
10-
<div class="span3 well">
11-
side nav
12-
</div>
13-
<div class="span9">
14-
infos
10+
<div class="hero-unit">
11+
<h1>Something broke</h1>
12+
<p>Feel free to file a <g:link url="https://github.com/tburch/jsonblob/issues">bug report</g:link> or open a <g:link url="https://github.com/tburch/jsonblob/pulls">pull request</g:link> with the fix.</p>
1513
</div>
1614
</div>
1715
</body>

grails-app/views/notfound.gsp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="layout" content="bootstrap"/>
5+
<title>It's gone</title>
6+
</head>
7+
8+
<body>
9+
<div class="row-fluid">
10+
<div class="hero-unit">
11+
<h1>This page doesn't exist</h1>
12+
<p>This probably means you've probably reached the end of the internet...</p>
13+
<p><g:link url="/" class="btn btn-primary btn-large">Home &raquo;</g:link></p>
14+
</div>
15+
</div>
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)