Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove error classes and use http-errors. #105

Merged
merged 2 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- The `processRequest` function now requires a [`http.ServerResponse`](https://nodejs.org/api/http.html#http_class_http_serverresponse) instance as its second argument.
- `Upload` scalar promises now resolve with a `createReadStream` method instead of a `stream` property, via [#92](https://github.com/jaydenseric/apollo-upload-server/pull/92).
- Replaced the previously exported error classes with [`http-errors`](https://npm.im/http-errors) and snapshot tested error details, via [#105](https://github.com/jaydenseric/apollo-upload-server/pull/105).

### Minor

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"busboy": "^0.2.14",
"fs-capacitor": "^1.0.0",
"http-errors": "^1.7.0",
"object-path": "^0.11.4"
},
"devDependencies": {
Expand Down
42 changes: 0 additions & 42 deletions src/errors.mjs

This file was deleted.

1 change: 0 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './types'
export * from './middleware'
export * from './errors'
70 changes: 33 additions & 37 deletions src/middleware.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import Busboy from 'busboy'
import objectPath from 'object-path'
import WriteStream from 'fs-capacitor'
import {
SPEC_URL,
ParseUploadError,
MaxFileSizeUploadError,
MaxFilesUploadError,
MapBeforeOperationsUploadError,
FilesBeforeMapUploadError,
FileMissingUploadError,
DisconnectUploadError
} from './errors'
import createError from 'http-errors'

/**
* GraphQL upload server options, mostly relating to security, performance and
Expand All @@ -36,6 +27,20 @@ import {
* @see [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests#postRequests).
*/

/**
* Official [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
* URL. Useful for error messages, etc.
* @kind constant
* @name SPEC_URL
* @type {string}
* @example <caption>How to import.</caption>
* ```js
* import { SPEC_URL } from 'apollo-upload-server'
* ```
*/
export const SPEC_URL =
'https://github.com/jaydenseric/graphql-multipart-request-spec'

/**
* An expected file upload.
* @kind class
Expand Down Expand Up @@ -169,19 +174,19 @@ export const processRequest = (
operationsPath = objectPath(operations)
} catch (error) {
exit(
new ParseUploadError(
`Invalid JSON in the ‘operations’ multipart field (${SPEC_URL}).`,
400
createError(
400,
`Invalid JSON in the ‘operations’ multipart field (${SPEC_URL}).`
)
)
}
break
case 'map': {
if (!operations)
return exit(
new MapBeforeOperationsUploadError(
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${SPEC_URL}).`,
400
createError(
400,
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${SPEC_URL}).`
)
)

Expand All @@ -190,9 +195,9 @@ export const processRequest = (
mapEntries = Object.entries(JSON.parse(value))
} catch (error) {
return exit(
new ParseUploadError(
`Invalid JSON in the ‘map’ multipart field (${SPEC_URL}).`,
400
createError(
400,
`Invalid JSON in the ‘map’ multipart field (${SPEC_URL}).`
)
)
}
Expand All @@ -201,10 +206,7 @@ export const processRequest = (
// parse might not match the map provided by the client.
if (mapEntries.length > maxFiles)
return exit(
new MaxFilesUploadError(
`${maxFiles} max file uploads exceeded.`,
413
)
createError(413, `${maxFiles} max file uploads exceeded.`)
)

map = new Map()
Expand All @@ -227,9 +229,9 @@ export const processRequest = (
stream.resume()

return exit(
new FilesBeforeMapUploadError(
`Misordered multipart fields; files should follow ‘map’ (${SPEC_URL}).`,
400
createError(
400,
`Misordered multipart fields; files should follow ‘map’ (${SPEC_URL}).`
)
)
}
Expand All @@ -252,10 +254,7 @@ export const processRequest = (
if (currentStream === stream) currentStream = null
stream.unpipe()
capacitor.destroy(
new MaxFileSizeUploadError(
'File truncated as it exceeds the size limit.',
413
)
createError(413, 'File truncated as it exceeds the size limit.')
)
})

Expand Down Expand Up @@ -293,9 +292,7 @@ export const processRequest = (
})

parser.once('filesLimit', () =>
exit(
new MaxFilesUploadError(`${maxFiles} max file uploads exceeded.`, 413)
)
exit(createError(413, `${maxFiles} max file uploads exceeded.`))
)

parser.once('finish', () => {
Expand All @@ -305,9 +302,7 @@ export const processRequest = (
if (map)
for (const upload of map.values())
if (!upload.file)
upload.reject(
new FileMissingUploadError('File missing in the request.', 400)
)
upload.reject(createError(400, 'File missing in the request.'))
})

parser.once('error', exit)
Expand All @@ -322,7 +317,8 @@ export const processRequest = (
request.once('close', () => {
if (!requestEnded)
exit(
new DisconnectUploadError(
createError(
499,
'Request disconnected during file upload stream parsing.'
)
)
Expand Down
Loading