Skip to content

Commit

Permalink
Completed error.json for English and added preferredErrorLanguage
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbankhead committed Feb 18, 2017
1 parent a5afc72 commit f684dde
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 44 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const jetta = require('jetta')

jetta.request('altusaero.com', (error, results) => {
if (error !== null) {
// Results contains useful error details to help debug
// Results contain useful error details to help debug
console.dir(results.error, {colors: true})
throw error
} else {
Expand Down Expand Up @@ -85,7 +85,9 @@ jetta.request(url, {useDefaultProgressLogger: true}, (error, results) => {
throw error
} else {
// do stuff with results...
fs.writeFile(path.basename(results.url.parsedURL.path), results.data, (error) => {
const filename = path.basename(results.url.parsedURL.path)

fs.writeFile(filename, results.data, (error) => {
if (error !== null) throw error
console.log('done.')
})
Expand All @@ -104,7 +106,7 @@ jetta.request(url, {useDefaultProgressLogger: true}, (error, results) => {
- receiving additional information behind an error
- sending an error directly to a client in their native language
- debugging
- every key corresponds to `results.error.type` ('http-request-error') from a request when an error has occurred
- every key corresponds to `results.error.type` from a request when an error has occurred ('http-request-error', 'http-too-many-redirects', etc.)
- every object has the following attributes:
- `details` BOOLEAN
- determines if an error includes an `Error` object with extended stack details
Expand Down Expand Up @@ -186,9 +188,7 @@ We wanted a simple, flexible, and easy-to-use request library that didn't compro


## Notes
- this project will move from **alpha** to **beta** after:
- `tests` are currently under development - after there are finalized
- Each error in `data/error.json` has a message in `en`.
- this project will move from **alpha** to **beta** after `tests` are completed.


## Credits
Expand Down
1 change: 1 addition & 0 deletions data/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dnt": 1,
"user-agent": "Mozilla/5.0"
},
"preferredErrorLanguage": "en",
"progressLog": null,
"redirectLimit": 10,
"requestOptions": {
Expand Down
54 changes: 27 additions & 27 deletions data/error.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,49 @@
"http-bad-response-code": {
"details": false,
"message": {
"en": ""
"en": "Request received a bad response code - check results.statusCode for more details."
}
},
"http-exceeded-data-limit-actual": {
"details": false,
"message": {
"en": ""
}
},
"http-stream-read-error": {
"details": true,
"message": {
"en": ""
"en": "The actual data recieved was higher than the data limit."
}
},
"http-exceeded-data-limit-content-length": {
"details": false,
"message": {
"en": ""
"en": "The content-length header was higher than the data limit - cancelled to not waste time."
}
},
"http-gunzip-failed": {
"details": true,
"message": {
"en": ""
"en": "While the request succeeded zlib.gunzip has failed. You can see the compressed data in request.data."
}
},
"http-inflate-failed": {
"details": true,
"message": {
"en": ""
"en": "While the request succeeded zlib.inflate has failed. You can see the compressed data in request.data."
}
},
"http-invalid-protocol": {
"details": false,
"message": {
"en": ""
"en": "The protocol provided is invalid. Try 'http:', 'https:', UNIX domain socket, or named pipe instead."
}
},
"http-invalid-url": {
"details": false,
"message": {
"en": ""
}
},
"http-no-data-received": {
"details": false,
"message": {
"en": ""
"en": "The URL provided is invalid."
}
},
"http-request-aborted-client": {
"details": false,
"message": {
"en": ""
"en": "The request was aborted from the client-side before the server responded."
}
},
"http-request-aborted-server": {
Expand All @@ -68,43 +56,55 @@
"http-request-error": {
"details": true,
"message": {
"en": ""
"en": "An error occurred with the request. See details for more information."
}
},
"http-request-timed-out-initial": {
"details": false,
"message": {
"en": ""
"en": "The request timed out while attempting to connect to the server."
}
},
"http-response-aborted-client": {
"details": true,
"message": {
"en": ""
"en": "The request was aborted from the client-side after the server responded."
}
},
"http-response-error": {
"details": true,
"message": {
"en": ""
"en": "An error occurred with the response. See details for more information."
}
},
"http-response-timed-out-during": {
"details": false,
"message": {
"en": ""
"en": "The request timed out during the response. The server may be busy or the connection has dropped."
}
},
"http-response-timed-out-initial": {
"details": false,
"message": {
"en": ""
"en": "The request timed out after the response, but before any data was recieved. The server may be busy or the connection has dropped."
}
},
"http-stream-read-error": {
"details": true,
"message": {
"en": "An error has occurred will reading from the given stream."
}
},
"http-too-many-redirects": {
"details": false,
"message": {
"en": ""
"en": "Too many response redirects."
}
},
"http-unknown-content-encoding": {
"details": false,
"message": {
"en": "Unrecognized content-encoding. The data is included if you want"
}
}
}
22 changes: 15 additions & 7 deletions docs/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
- `headers['content-type']`: 'application/json'
- `method`: POST

- `preferredErrorLanguage` STRING
- The preferred language for any error messages that may occur.
- Should be an ISO 639-1 code, such as 'en' for English and 'fr' for French
- Falls back to English if preferred language unavailable

- `progressLog` OBJECT
- An object with with a `log` method
- See `jetta.ProgressLogger` for details and ideas for using a custom progress logger
Expand Down Expand Up @@ -159,11 +164,12 @@

- `callback` FUNCTION (error, results)
- Returns `error` and `results` of the request
- The `error` argument is used for compatibility purposes as `results.error` carries a little more information
- See `jetta.error` for additional details on `results.error`

- _NOTE_: `results.error` may carry a bit more information

- `error` OBJECT
- if `null`, the request did not have an error
- else, an `Error` object, using `preferredErrorLanguage` if available

- `results` OBJECT
- The results of the response
Expand Down Expand Up @@ -194,7 +200,7 @@

- `error` OBJECT
- The error data, if any
- If `null`, great
- If `null`, _great_
- Else, an OBJECT with:
- `type` STRING
- A precise error code used in jetta
Expand All @@ -205,16 +211,18 @@
const messageInEnglish = jetta.error[results.error.type].message.en
```

- See `jetta.error` for additional details

- `details` OBJECT
- The native `Error` object
- Due to the nature of requests this may not always be available, so use `type` whenever `jetta.error[results.error.type].details === false`.
- Due to the nature of requests this may not always be available, thus set to `null`

- _NOTE_: correlates with `jetta.error[results.error.type].details === false`.

- See `jetta.error` for additional details
- `message` STRING
- The error message, using `preferredErrorLanguage` if available

- `options` OBJECT
- The options used for this request _before_ merging parameters
- Useful for debugging

- _NOTE_: See `requestOptionsFinal` to see the finalized options used in the request

Expand Down
24 changes: 20 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const querystring = require('querystring')
const zlib = require('zlib')

const defaults = require('../data/defaults')
const errorTypes = require('../data/error')
const ProgressLogger = require('./progress-logger')
const urlParser = require('./url-parser')

Expand All @@ -15,9 +16,21 @@ function processCallback (data, callback) {
callback(null, data)
} else {
if (typeof data.error.details === 'object') {
data.error.message = data.error.details.message

callback(data.error.details, data)
} else {
callback(new Error(data.error.type), data)
const errorMessageInLangs = errorTypes[data.error.type].message

data.error.details = null

if (typeof errorMessageInLangs[data.options.preferredErrorLanguage] === 'string') {
data.error.message = `${data.error.type}: ${errorMessageInLangs[data.options.preferredErrorLanguage]}`
} else {
data.error.message = `${data.error.type}: ${errorMessageInLangs.en}`
}

callback(new Error(data.error.message), data)
}
}
}
Expand Down Expand Up @@ -55,6 +68,7 @@ function decompressData (results, callback) {
})
break
default:
results.error = {type: 'http-unknown-content-encoding'}
processCallback(results, callback)
}
}
Expand Down Expand Up @@ -259,7 +273,11 @@ function httpRequest (givenURL, options, callback, currentRedirects) {
total: Date.now() - startTotalTime,
response: Date.now() - startResponseTime
}
results.data = Buffer.concat(dataRecieved, results.dataLength)

if (results.dataLength !== 0) {
results.data = Buffer.concat(dataRecieved, results.dataLength)
}

results.statusCode = response.statusCode

if (response.statusCode > 299 && response.headers.location && results.options.redirectLimit > results.redirects && results.error === null) {
Expand All @@ -273,8 +291,6 @@ function httpRequest (givenURL, options, callback, currentRedirects) {
results.error = {type: 'http-too-many-redirects'}
} else if (response.statusCode > 299 && results.error === null) {
results.error = {type: 'http-bad-response-code'}
} else if (results.dataLength === 0 && results.error === null) {
results.error = {type: 'http-no-data-received'}
}
processResults()
})
Expand Down

0 comments on commit f684dde

Please sign in to comment.