Skip to content

Commit 3fd3bce

Browse files
committed
Simplify promise-based handlers using async-await syntax
1 parent 20e0af1 commit 3fd3bce

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
lines changed

lib/handlers/delete.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ module.exports = handler
22

33
const debug = require('../debug').handlers
44

5-
function handler (req, res, next) {
5+
async function handler (req, res, next) {
66
debug('DELETE -- Request on' + req.originalUrl)
77

88
const ldp = req.app.locals.ldp
9-
ldp.delete(req)
10-
.then(() => {
11-
debug('DELETE -- Ok.')
12-
res.sendStatus(200)
13-
next()
14-
})
15-
.catch(err => {
16-
debug('DELETE -- Failed to delete: ' + err)
17-
next(err)
18-
})
9+
try {
10+
await ldp.delete(req)
11+
debug('DELETE -- Ok.')
12+
res.sendStatus(200)
13+
next()
14+
} catch (err) {
15+
debug('DELETE -- Failed to delete: ' + err)
16+
next(err)
17+
}
1918
}

lib/handlers/patch/n3-patch-parser.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,35 @@ const PATCH_NS = 'http://www.w3.org/ns/solid/terms#'
99
const PREFIXES = `PREFIX solid: <${PATCH_NS}>\n`
1010

1111
// Parses the given N3 patch document
12-
function parsePatchDocument (targetURI, patchURI, patchText) {
12+
async function parsePatchDocument (targetURI, patchURI, patchText) {
1313
// Parse the N3 document into triples
14-
return new Promise((resolve, reject) => {
15-
const patchGraph = $rdf.graph()
14+
const patchGraph = $rdf.graph()
15+
try {
1616
$rdf.parse(patchText, patchGraph, patchURI, 'text/n3')
17-
resolve(patchGraph)
18-
})
19-
.catch(err => { throw error(400, `Patch document syntax error: ${err}`) })
17+
} catch (err) {
18+
throw error(400, `Patch document syntax error: ${err}`)
19+
}
2020

2121
// Query the N3 document for insertions and deletions
22-
.then(patchGraph => queryForFirstResult(patchGraph, `${PREFIXES}
22+
let firstResult;
23+
try {
24+
firstResult = await queryForFirstResult(patchGraph, `${PREFIXES}
2325
SELECT ?insert ?delete ?where WHERE {
2426
?patch solid:patches <${targetURI}>.
2527
OPTIONAL { ?patch solid:inserts ?insert. }
2628
OPTIONAL { ?patch solid:deletes ?delete. }
2729
OPTIONAL { ?patch solid:where ?where. }
2830
}`)
29-
.catch(err => { throw error(400, `No patch for ${targetURI} found.`, err) })
30-
)
31+
} catch (err) {
32+
throw error(400, `No patch for ${targetURI} found.`, err)
33+
}
3134

3235
// Return the insertions and deletions as an rdflib patch document
33-
.then(result => {
34-
const {'?insert': insert, '?delete': deleted, '?where': where} = result
35-
if (!insert && !deleted) {
36-
throw error(400, 'Patch should at least contain inserts or deletes.')
37-
}
38-
return {insert, delete: deleted, where}
39-
})
36+
const {'?insert': insert, '?delete': deleted, '?where': where} = firstResult
37+
if (!insert && !deleted) {
38+
throw error(400, 'Patch should at least contain inserts or deletes.')
39+
}
40+
return {insert, delete: deleted, where}
4041
}
4142

4243
// Queries the store with the given SPARQL query and returns the first result

lib/handlers/patch/sparql-update-parser.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ const $rdf = require('rdflib')
66
const error = require('../../http-error')
77

88
// Parses the given SPARQL UPDATE document
9-
function parsePatchDocument (targetURI, patchURI, patchText) {
10-
return new Promise((resolve, reject) => {
11-
const baseURI = patchURI.replace(/#.*/, '')
12-
try {
13-
resolve($rdf.sparqlUpdateParser(patchText, $rdf.graph(), baseURI))
14-
} catch (err) {
15-
reject(error(400, `Patch document syntax error: ${err}`))
16-
}
17-
})
9+
async function parsePatchDocument (targetURI, patchURI, patchText) {
10+
const baseURI = patchURI.replace(/#.*/, '')
11+
try {
12+
return $rdf.sparqlUpdateParser(patchText, $rdf.graph(), baseURI)
13+
} catch (err) {
14+
throw error(400, `Patch document syntax error: ${err}`)
15+
}
1816
}

lib/handlers/put.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ module.exports = handler
22

33
const debug = require('debug')('solid:put')
44

5-
function handler (req, res, next) {
5+
async function handler (req, res, next) {
66
const ldp = req.app.locals.ldp
77
debug(req.originalUrl)
88
res.header('MS-Author-Via', 'SPARQL')
99

10-
ldp.put(req, req)
11-
.then(() => {
12-
debug('succeded putting the file')
10+
try {
11+
await ldp.put(req, req)
12+
debug('succeded putting the file')
1313

14-
res.sendStatus(201)
15-
return next()
16-
})
17-
.catch(err => {
18-
debug('error putting the file:' + err.message)
19-
err.message = 'Can\'t write file: ' + err.message
20-
return next(err)
21-
})
14+
res.sendStatus(201)
15+
return next()
16+
} catch (err) {
17+
debug('error putting the file:' + err.message)
18+
err.message = 'Can\'t write file: ' + err.message
19+
return next(err)
20+
}
2221
}

0 commit comments

Comments
 (0)