-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmytro Panontko
committed
Apr 25, 2019
1 parent
191ac98
commit af21a8c
Showing
13 changed files
with
414 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { Component } from 'react'; | ||
|
||
import { Button } from 'Form'; | ||
|
||
class ExampleComponents extends Component { | ||
state = { | ||
error: '', | ||
} | ||
|
||
showError = () => { | ||
fetch('/api/test/error') | ||
.then(response => response.json()) | ||
.then(data => this.setState({ error: JSON.stringify(data) })); | ||
} | ||
|
||
render() { | ||
const { error } = this.state; | ||
|
||
return ( | ||
<div className="container"> | ||
<Button className="red" onClick={this.showError}>Show Error Message</Button> | ||
<p> | ||
{`Error: ${error}`} | ||
</p> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ExampleComponents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Error handling | ||
|
||
This is a document how we most work with **Error** on current project. | ||
|
||
For correct works with **Error** in project please read [this article](https://expressjs.com/en/guide/error-handling.html) and use `Kit.CustomError` as `Error` object | ||
|
||
Example of handling: | ||
|
||
```js | ||
router.get('/error', (req, res, next) => { | ||
try { | ||
// ...some code | ||
} catch(e) { | ||
next(new Kit.CustomError('UNAUTHORIZED_ACCESS', 401)); | ||
} | ||
}); | ||
``` | ||
|
||
## Error structure | ||
|
||
This structure you must send to client when error is happened and you can get this structure from `Kit.CustomError` by `.get()` method | ||
|
||
```js | ||
{ | ||
"errors": [ | ||
{ | ||
"parameter": "start_time", | ||
"details": "invalid date", | ||
"code": "INVALID_PARAMETER", | ||
"value": "", | ||
"message": "Expected time, got \"\" for start_time", | ||
"userMessage": "Expected time, got \"\" for start_time" | ||
} | ||
], | ||
"request": { | ||
"params": { | ||
"account_id": "hkk5" | ||
} | ||
}, | ||
"metadata": {} | ||
} | ||
``` | ||
|
||
## Error codes & what they mean | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>HTTP Code</th> | ||
<th>Error Code</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>403</td> | ||
<td>ACCOUNT_NOT_FOUND</td> | ||
</tr> | ||
<tr> | ||
<td>403</td> | ||
<td>ACTION_NOT_ALLOWED</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>EXCLUSIVE_PARAMETERS</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>FEATURE_NOT_AVAILABLE</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>ILLEGAL_CHARACTERS</td> | ||
</tr> | ||
<tr> | ||
<td>500</td> | ||
<td>INTERNAL_ERROR</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>INVALID_PARAMETER</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>INVALID_USER</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>INVALID_USER_ID</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>MISSING_PARAMETERS</td> | ||
</tr> | ||
<tr> | ||
<td>404</td> | ||
<td>NOT_FOUND</td> | ||
</tr> | ||
<tr> | ||
<td>400</td> | ||
<td>REQUEST_TOO_COMPLEX</td> | ||
</tr> | ||
<tr> | ||
<td>404</td> | ||
<td>ROUTE_NOT_FOUND</td> | ||
</tr> | ||
<tr> | ||
<td>503</td> | ||
<td>SERVICE_UNAVAILABLE</td> | ||
</tr> | ||
<tr> | ||
<td>503</td> | ||
<td>OVER_CAPACITY</td> | ||
</tr> | ||
<tr> | ||
<td>429</td> | ||
<td>TOO_MANY_REQUESTS</td> | ||
</tr> | ||
<tr> | ||
<td>401</td> | ||
<td>UNAUTHORIZED_ACCESS</td> | ||
</tr> | ||
<tr> | ||
<td>403</td> | ||
<td>USER_NOT_FOUND</td> | ||
</tr> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
module.exports = { | ||
ACCOUNT_NOT_FOUND: { | ||
message: 'Account not found', | ||
userMessage: 'Account not found', | ||
code: 403, | ||
}, | ||
ACTION_NOT_ALLOWED: { | ||
message: 'Action not allowed', | ||
userMessage: 'Action not allowed', | ||
code: 403, | ||
}, | ||
EXCLUSIVE_PARAMETERS: { | ||
message: 'Exclusive parameters', | ||
userMessage: 'Exclusive parameters', | ||
code: 400, | ||
}, | ||
FEATURE_NOT_AVAILABLE: { | ||
message: 'Feature not available', | ||
userMessage: 'Feature not available', | ||
code: 400, | ||
}, | ||
ILLEGAL_CHARACTERS: { | ||
message: 'Illegal characters', | ||
userMessage: 'Illegal characters', | ||
code: 400, | ||
}, | ||
INTERNAL_ERROR: { | ||
message: 'Server error', | ||
userMessage: 'Server error', | ||
code: 500, | ||
}, | ||
INVALID_PARAMETER: { | ||
message: 'Invalid parameter', | ||
userMessage: 'Invalid parameter', | ||
code: 400, | ||
}, | ||
INVALID_USER: { | ||
message: 'Invalid user', | ||
userMessage: 'Invalid user', | ||
code: 400, | ||
}, | ||
INVALID_USER_ID: { | ||
message: 'Invalid user ID', | ||
userMessage: 'Invalid user ID', | ||
code: 400, | ||
}, | ||
MISSING_PARAMETERS: { | ||
message: 'Missing parameters', | ||
userMessage: 'Missing parameters', | ||
code: 400, | ||
}, | ||
NOT_FOUND: { | ||
message: 'Not Found', | ||
userMessage: 'Not Found', | ||
code: 404, | ||
}, | ||
REQUEST_TOO_COMPLEX: { | ||
message: 'Request too complex', | ||
userMessage: 'Request too complex', | ||
code: 400, | ||
}, | ||
ROUTE_NOT_FOUND: { | ||
message: 'Route not found', | ||
userMessage: 'Route not found', | ||
code: 404, | ||
}, | ||
SERVICE_UNAVAILABLE: { | ||
message: 'Service unavailable', | ||
userMessage: 'Service unavailable', | ||
code: 503, | ||
}, | ||
OVER_CAPACITY: { | ||
message: 'Over capacity', | ||
userMessage: 'Over capacity', | ||
code: 503, | ||
}, | ||
TOO_MANY_REQUESTS: { | ||
message: 'Too many request', | ||
userMessage: 'Too many request', | ||
code: 429, | ||
}, | ||
UNAUTHORIZED_ACCESS: { | ||
message: 'Unathorized access', | ||
userMessage: 'Unathorized access', | ||
code: 401, | ||
}, | ||
USER_NOT_FOUND: { | ||
message: 'User not found', | ||
userMessage: 'User not found', | ||
code: 403, | ||
}, | ||
FORBIDDEN: { | ||
message: 'Forbidden', | ||
userMessage: 'Forbidden', | ||
code: 403, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function errorHandling(err, req, res, next) { | ||
if (res.headersSent) { | ||
return next(err); | ||
} | ||
|
||
res.status(err.code); | ||
return res.json(err.get()); | ||
} | ||
|
||
// process.on('unhandledRejection', (err) => { | ||
// console.error(err); | ||
// }); | ||
|
||
// process.on('uncaughtException', (err) => { | ||
// console.error(err); | ||
// process.exit(1); | ||
// }); | ||
|
||
module.exports = errorHandling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const express = require('express'); | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/error', (req, res, next) => { | ||
next(new Kit.CustomError('UNAUTHORIZED_ACCESS', 401)); | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.