Skip to content

Commit

Permalink
errorMessages --> httpErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Jun 12, 2023
1 parent e78a485 commit 77bc67c
Show file tree
Hide file tree
Showing 157 changed files with 250 additions and 253 deletions.
2 changes: 1 addition & 1 deletion core/base-service/base-graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BaseGraphqlService extends BaseService {
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages: httpErrorMessages,
httpErrors: httpErrorMessages,
systemErrors,
})
const json = transformJson(this._parseJson(buffer))
Expand Down
6 changes: 3 additions & 3 deletions core/base-service/base-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BaseJsonService extends BaseService {
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.errorMessages={}] Key-value map of status codes
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
Expand All @@ -47,7 +47,7 @@ class BaseJsonService extends BaseService {
schema,
url,
options = {},
errorMessages = {},
httpErrors = {},
systemErrors = {},
}) {
const mergedOptions = {
Expand All @@ -57,7 +57,7 @@ class BaseJsonService extends BaseService {
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
httpErrors,
systemErrors,
})
const json = this._parseJson(buffer)
Expand Down
6 changes: 3 additions & 3 deletions core/base-service/base-svg-scraping.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BaseSvgScrapingService extends BaseService {
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.errorMessages={}] Key-value map of status codes
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
Expand All @@ -71,7 +71,7 @@ class BaseSvgScrapingService extends BaseService {
valueMatcher,
url,
options = {},
errorMessages = {},
httpErrors = {},
systemErrors = {},
}) {
const logTrace = (...args) => trace.logTrace('fetch', ...args)
Expand All @@ -82,7 +82,7 @@ class BaseSvgScrapingService extends BaseService {
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
httpErrors,
systemErrors,
})
logTrace(emojic.dart, 'Response SVG', buffer)
Expand Down
6 changes: 3 additions & 3 deletions core/base-service/base-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BaseXmlService extends BaseService {
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.errorMessages={}] Key-value map of status codes
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
Expand All @@ -44,7 +44,7 @@ class BaseXmlService extends BaseService {
schema,
url,
options = {},
errorMessages = {},
httpErrors = {},
systemErrors = {},
parserOptions = {},
}) {
Expand All @@ -56,7 +56,7 @@ class BaseXmlService extends BaseService {
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
httpErrors,
systemErrors,
})
const validateResult = XMLValidator.validate(buffer)
Expand Down
6 changes: 3 additions & 3 deletions core/base-service/base-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BaseYamlService extends BaseService {
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.errorMessages={}] Key-value map of status codes
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
Expand All @@ -41,7 +41,7 @@ class BaseYamlService extends BaseService {
schema,
url,
options = {},
errorMessages = {},
httpErrors = {},
systemErrors = {},
encoding = 'utf8',
}) {
Expand All @@ -58,7 +58,7 @@ class BaseYamlService extends BaseService {
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
httpErrors,
systemErrors,
})
let parsed
Expand Down
4 changes: 2 additions & 2 deletions core/base-service/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class BaseService {
this._metricHelper = metricHelper
}

async _request({ url, options = {}, errorMessages = {}, systemErrors = {} }) {
async _request({ url, options = {}, httpErrors = {}, systemErrors = {} }) {
const logTrace = (...args) => trace.logTrace('fetch', ...args)
let logUrl = url
const logOptions = Object.assign({}, options)
Expand All @@ -253,7 +253,7 @@ class BaseService {
)
await this._meterResponse(res, buffer)
logTrace(emojic.dart, 'Response status code', res.statusCode)
return checkErrorResponse(errorMessages)({ buffer, res })
return checkErrorResponse(httpErrors)({ buffer, res })
}

static enabledMetrics = []
Expand Down
10 changes: 5 additions & 5 deletions core/base-service/check-error-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const defaultErrorMessages = {
429: 'rate limited by upstream service',
}

export default function checkErrorResponse(errorMessages = {}) {
export default function checkErrorResponse(httpErrors = {}) {
return async function ({ buffer, res }) {
let error
errorMessages = { ...defaultErrorMessages, ...errorMessages }
httpErrors = { ...defaultErrorMessages, ...httpErrors }
if (res.statusCode === 404) {
error = new NotFound({ prettyMessage: errorMessages[404] })
error = new NotFound({ prettyMessage: httpErrors[404] })
} else if (res.statusCode !== 200) {
const underlying = Error(
`Got status code ${res.statusCode} (expected 200)`
)
const props = { underlyingError: underlying }
if (errorMessages[res.statusCode] !== undefined) {
props.prettyMessage = errorMessages[res.statusCode]
if (httpErrors[res.statusCode] !== undefined) {
props.prettyMessage = httpErrors[res.statusCode]
}
if (res.statusCode >= 500) {
error = new Inaccessible(props)
Expand Down
4 changes: 2 additions & 2 deletions doc/TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ Description of the code:

- `_requestJson()` automatically adds an Accept header, checks the status code, parses the response as JSON, and returns the parsed response.
- `_requestJson()` uses [got](https://github.com/sindresorhus/got) to perform the HTTP request. Options can be passed to got, including method, query string, and headers. If headers are provided they will override the ones automatically set by `_requestJson()`. There is no need to specify json, as the JSON parsing is handled by `_requestJson()`. See the `got` docs for [supported options](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md).
- Error messages corresponding to each status code can be returned by passing a dictionary of status codes -> messages in `errorMessages`.
- Error messages corresponding to each status code can be returned by passing a dictionary of status codes -> messages in `httpErrors`.
- A more complex call to `_requestJson()` might look like this:
```js
return this._requestJson({
schema: mySchema,
url,
options: { searchParams: { branch: 'master' } },
errorMessages: {
httpErrors: {
401: 'private application not supported',
404: 'application not found',
},
Expand Down
2 changes: 1 addition & 1 deletion doc/service-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ npm run test:services -- --only="wercker" --fgrep="Build status (with branch)"
Having covered the typical and custom cases, we'll move on to errors. We should include a test for the 'not found' response and also tests for any other custom error handling. The Wercker integration defines a custom error condition for 401 as well as a custom 404 message:

```js
errorMessages: {
httpErrors: {
401: 'private application not supported',
404: 'application not found',
}
Expand Down
2 changes: 1 addition & 1 deletion services/appveyor/appveyor-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class AppVeyorBase extends BaseJsonService {
return this._requestJson({
schema,
url,
errorMessages: { 404: 'project not found or access denied' },
httpErrors: { 404: 'project not found or access denied' },
})
}

Expand Down
8 changes: 4 additions & 4 deletions services/azure-devops/azure-devops-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export default class AzureDevOpsBase extends BaseJsonService {
defaultToEmptyStringForUser: true,
}

async fetch({ url, options, schema, errorMessages }) {
async fetch({ url, options, schema, httpErrors }) {
return this._requestJson(
this.authHelper.withBasicAuth({
schema,
url,
options,
errorMessages,
httpErrors,
})
)
}
Expand All @@ -35,7 +35,7 @@ export default class AzureDevOpsBase extends BaseJsonService {
project,
definitionId,
branch,
errorMessages
httpErrors
) {
// Microsoft documentation: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-5.0
const url = `https://dev.azure.com/${organization}/${project}/_apis/build/builds`
Expand All @@ -56,7 +56,7 @@ export default class AzureDevOpsBase extends BaseJsonService {
url,
options,
schema: latestBuildSchema,
errorMessages,
httpErrors,
})

if (json.count !== 1) {
Expand Down
2 changes: 1 addition & 1 deletion services/azure-devops/azure-devops-build.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class AzureDevOpsBuild extends BaseSvgScrapingService {
stageName: stage,
jobName: job,
},
errorMessages: {
httpErrors: {
404: 'user or project not found',
},
})
Expand Down
6 changes: 3 additions & 3 deletions services/azure-devops/azure-devops-coverage.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ export default class AzureDevOpsCoverage extends AzureDevOpsBase {
}

async handle({ organization, project, definitionId, branch }) {
const errorMessages = {
const httpErrors = {
404: 'build pipeline or coverage not found',
}
const buildId = await this.getLatestCompletedBuildId(
organization,
project,
definitionId,
branch,
errorMessages
httpErrors
)
// Microsoft documentation: https://docs.microsoft.com/en-us/rest/api/azure/devops/test/code%20coverage/get%20build%20code%20coverage?view=azure-devops-rest-5.0
const url = `https://dev.azure.com/${organization}/${project}/_apis/test/codecoverage`
Expand All @@ -110,7 +110,7 @@ export default class AzureDevOpsCoverage extends AzureDevOpsBase {
url,
options,
schema: buildCodeCoverageSchema,
errorMessages,
httpErrors,
})

let covered = 0
Expand Down
7 changes: 2 additions & 5 deletions services/azure-devops/azure-devops-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ const schema = Joi.object({
.required(),
}).required()

async function fetch(
serviceInstance,
{ url, searchParams = {}, errorMessages }
) {
async function fetch(serviceInstance, { url, searchParams = {}, httpErrors }) {
// Microsoft documentation: https://docs.microsoft.com/en-us/rest/api/vsts/build/status/get
const { message: status } = await serviceInstance._requestSvg({
schema,
url,
options: { searchParams },
errorMessages,
httpErrors,
})
return { status }
}
Expand Down
2 changes: 1 addition & 1 deletion services/azure-devops/azure-devops-release.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class AzureDevOpsRelease extends BaseSvgScrapingService {
// Microsoft documentation: ?
const props = await fetch(this, {
url: `https://vsrm.dev.azure.com/${organization}/_apis/public/Release/badge/${projectId}/${definitionId}/${environmentId}`,
errorMessages: {
httpErrors: {
400: 'project not found',
404: 'user or environment not found',
500: 'inaccessible or definition not found',
Expand Down
6 changes: 3 additions & 3 deletions services/azure-devops/azure-devops-tests.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
}

async fetchTestResults({ organization, project, definitionId, branch }) {
const errorMessages = {
const httpErrors = {
404: 'build pipeline or test result summary not found',
}
const buildId = await this.getLatestCompletedBuildId(
organization,
project,
definitionId,
branch,
errorMessages
httpErrors
)

// https://dev.azure.com/azuredevops-powershell/azuredevops-powershell/_apis/test/ResultSummaryByBuild?buildId=20
Expand All @@ -163,7 +163,7 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
searchParams: { buildId },
},
schema: buildTestResultSummarySchema,
errorMessages,
httpErrors,
})
}

Expand Down
2 changes: 1 addition & 1 deletion services/bit/bit-components.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class BitComponents extends BaseJsonService {
return this._requestJson({
url,
schema: collectionSchema,
errorMessages: {
httpErrors: {
404: 'collection not found',
},
})
Expand Down
2 changes: 1 addition & 1 deletion services/bitbucket/bitbucket-issues.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function issueClassGenerator(raw) {
options: {
searchParams: { limit: 0, q: '(state = "new" OR state = "open")' },
},
errorMessages: { 403: 'private repo' },
httpErrors: { 403: 'private repo' },
})
}

Expand Down
2 changes: 1 addition & 1 deletion services/bitbucket/bitbucket-pipelines.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BitbucketPipelines extends BaseJsonService {
'target.ref_name': branch,
},
},
errorMessages: { 403: 'private repo' },
httpErrors: { 403: 'private repo' },
})
}

Expand Down
6 changes: 3 additions & 3 deletions services/bitbucket/bitbucket-pull-request.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const queryParamSchema = Joi.object({
server: optionalUrl,
}).required()

const errorMessages = {
const httpErrors = {
401: 'invalid credentials',
403: 'private repo',
404: 'not found',
Expand Down Expand Up @@ -87,7 +87,7 @@ function pullRequestClassGenerator(raw) {
url: `https://bitbucket.org/api/2.0/repositories/${user}/${repo}/pullrequests/`,
schema,
options: { searchParams: { state: 'OPEN', limit: 0 } },
errorMessages,
httpErrors,
})
)
}
Expand All @@ -106,7 +106,7 @@ function pullRequestClassGenerator(raw) {
withAttributes: false,
},
},
errorMessages,
httpErrors,
})
)
}
Expand Down
2 changes: 1 addition & 1 deletion services/bitrise/bitrise.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class Bitrise extends BaseJsonService {
)}/status.json`,
options: { searchParams: { token, branch } },
schema,
errorMessages: {
httpErrors: {
403: 'app not found or invalid token',
},
})
Expand Down
2 changes: 1 addition & 1 deletion services/bower/bower-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class BaseBowerService extends LibrariesIoBase {
return this._requestJson({
schema,
url: `/bower/${packageName}`,
errorMessages: {
httpErrors: {
404: 'package not found',
},
})
Expand Down
Loading

0 comments on commit 77bc67c

Please sign in to comment.