Skip to content

Commit 426f2e4

Browse files
committed
Provide error handling middleware function in mu-helpers
1 parent 55e1eea commit 426f2e4

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Create a new folder. Add the following Dockerfile:
1111

1212
Create your microservice in `app.js`:
1313

14-
import { app, query } from 'mu';
14+
import { app, query, errorHandler } from 'mu';
1515

1616
app.get('/', function( req, res ) {
1717
res.send('Hello mu-javascript-template');
@@ -36,6 +36,8 @@ Create your microservice in `app.js`:
3636
});
3737
} );
3838

39+
app.use(errorHandler);
40+
3941
Check [Express' Getting Started guide](https://expressjs.com/en/starter/basic-routing.html) to learn how to build a REST API in Express.
4042

4143
## Requirements
@@ -50,6 +52,7 @@ The following importable variables are available:
5052
- `query(query) => Promise`: Function for sending queries to the triplestore
5153
- `update(query) => Promise`: Function for sending updates to the triplestore
5254
- `uuid()` => string: Generates a random UUID
55+
- `errorHandler(err, req, res, next)`: [Error handling middleware function for Express](https://expressjs.com/en/guide/error-handling.html). It needs to be loaded at the end.
5356
- `sparql`: [Template tag](https://www.npmjs.com/package/sparql-client-2#using-the-sparql-template-tag) to create queries with interpolated values
5457
- `sparqlEscapeString(value) => string`: Function to escape a string in SPARQL
5558
- `sparqlEscapeUri(value) => string`: Function to escape a URI in SPARQL

helpers/mu/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import app from './server';
1+
import { app, errorHandler } from './server';
22
import sparql from './sparql';
33
import uuidV1 from 'uuid/v1';
44

@@ -19,7 +19,8 @@ const mu = {
1919
sparqlEscapeDate: sparql.sparqlEscapeDate,
2020
sparqlEscapeDateTime: sparql.sparqlEscapeDateTime,
2121
sparqlEscapeBool: sparql.sparqlEscapeBool,
22-
uuid: uuid
22+
uuid,
23+
errorHandler
2324
};
2425

2526
const SPARQL = mu.SPARQL,
@@ -48,7 +49,8 @@ export {
4849
sparqlEscapeDate,
4950
sparqlEscapeDateTime,
5051
sparqlEscapeBool,
51-
uuid
52+
uuid,
53+
errorHandler
5254
};
5355

5456
export default mu;

helpers/mu/server.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,21 @@ app.use('/', function(req, res, next) {
1313
next();
1414
});
1515

16-
// development error handler - printing stacktrace
17-
if (app.get('env') === 'development') {
18-
app.use(function(err, req, res, next) {
19-
res.status(err.status || 400);
20-
res.json({
21-
errors: [ {title: err.message} ]
22-
});
23-
});
24-
}
25-
26-
// production error handler - no stacktrace
27-
app.use(function(err, req, res, next) {
16+
const errorHandler = function(err, req, res, next) {
2817
res.status(err.status || 400);
2918
res.json({
3019
errors: [ {title: err.message} ]
3120
});
32-
});
21+
};
3322

3423
// start server
3524
app.listen( 80, function() {
3625
console.log(`Starting server on port 80 in ${app.get('env')} mode`);
3726
});
3827

3928
export default app;
29+
30+
export {
31+
app,
32+
errorHandler
33+
}

0 commit comments

Comments
 (0)