diff --git a/__tests__/commands/changelogs/index.test.ts b/__tests__/commands/changelogs/index.test.ts index 1db515865..fab366e59 100644 --- a/__tests__/commands/changelogs/index.test.ts +++ b/__tests__/commands/changelogs/index.test.ts @@ -7,7 +7,7 @@ import nock from 'nock'; import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/changelogs.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -276,7 +276,9 @@ describe('rdme changelogs', () => { message: `Error uploading ${chalk.underline(`${fullDirectory}/${slug}.md`)}:\n\n${errorObject.message}`, }; - await expect(run([`./${fullDirectory}`, '--key', key])).rejects.toStrictEqual(new APIError(formattedErrorObject)); + await expect(run([`./${fullDirectory}`, '--key', key])).rejects.toStrictEqual( + new APIv1Error(formattedErrorObject), + ); getMocks.done(); postMocks.done(); diff --git a/__tests__/commands/changelogs/single.test.ts b/__tests__/commands/changelogs/single.test.ts index 0048083d9..765c74960 100644 --- a/__tests__/commands/changelogs/single.test.ts +++ b/__tests__/commands/changelogs/single.test.ts @@ -7,7 +7,7 @@ import nock from 'nock'; import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/changelogs.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -122,7 +122,7 @@ describe('rdme changelogs (single)', () => { message: `Error uploading ${chalk.underline(`${filePath}`)}:\n\n${errorObject.message}`, }; - await expect(run([filePath, '--key', key])).rejects.toStrictEqual(new APIError(formattedErrorObject)); + await expect(run([filePath, '--key', key])).rejects.toStrictEqual(new APIv1Error(formattedErrorObject)); getMock.done(); }); diff --git a/__tests__/commands/custompages/index.test.ts b/__tests__/commands/custompages/index.test.ts index a96353085..d89f989f4 100644 --- a/__tests__/commands/custompages/index.test.ts +++ b/__tests__/commands/custompages/index.test.ts @@ -7,7 +7,7 @@ import nock from 'nock'; import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/custompages.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -308,7 +308,9 @@ describe('rdme custompages', () => { message: `Error uploading ${chalk.underline(`${fullDirectory}/${slug}.md`)}:\n\n${errorObject.message}`, }; - await expect(run([`./${fullDirectory}`, '--key', key])).rejects.toStrictEqual(new APIError(formattedErrorObject)); + await expect(run([`./${fullDirectory}`, '--key', key])).rejects.toStrictEqual( + new APIv1Error(formattedErrorObject), + ); getMocks.done(); postMocks.done(); diff --git a/__tests__/commands/custompages/single.test.ts b/__tests__/commands/custompages/single.test.ts index 2e452963b..bffb22dfa 100644 --- a/__tests__/commands/custompages/single.test.ts +++ b/__tests__/commands/custompages/single.test.ts @@ -7,7 +7,7 @@ import nock from 'nock'; import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/custompages.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -154,7 +154,7 @@ describe('rdme custompages (single)', () => { message: `Error uploading ${chalk.underline(`${filePath}`)}:\n\n${errorObject.message}`, }; - await expect(run([filePath, '--key', key])).rejects.toStrictEqual(new APIError(formattedErrorObject)); + await expect(run([filePath, '--key', key])).rejects.toStrictEqual(new APIv1Error(formattedErrorObject)); getMock.done(); }); diff --git a/__tests__/commands/docs/index.test.ts b/__tests__/commands/docs/index.test.ts index d5a753d3a..3eb208fef 100644 --- a/__tests__/commands/docs/index.test.ts +++ b/__tests__/commands/docs/index.test.ts @@ -11,7 +11,7 @@ import prompts from 'prompts'; import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect, vi, type MockInstance } from 'vitest'; import Command from '../../../src/commands/docs/index.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import { after, before } from '../../helpers/get-gha-setup.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; @@ -349,7 +349,7 @@ describe('rdme docs', () => { }; await expect(run([`./${fullDirectory}`, '--key', key, '--version', version])).rejects.toStrictEqual( - new APIError(formattedErrorObject), + new APIv1Error(formattedErrorObject), ); getMocks.done(); diff --git a/__tests__/commands/docs/single.test.ts b/__tests__/commands/docs/single.test.ts index 9aeea4cd8..e9e30fef2 100644 --- a/__tests__/commands/docs/single.test.ts +++ b/__tests__/commands/docs/single.test.ts @@ -7,7 +7,7 @@ import nock from 'nock'; import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/docs/index.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env.js'; @@ -170,7 +170,7 @@ describe('rdme docs (single)', () => { }; await expect(run([filePath, '--key', key, '--version', version])).rejects.toStrictEqual( - new APIError(formattedErrorObject), + new APIv1Error(formattedErrorObject), ); getMock.done(); diff --git a/__tests__/commands/login.test.ts b/__tests__/commands/login.test.ts index 501be9db1..eca319582 100644 --- a/__tests__/commands/login.test.ts +++ b/__tests__/commands/login.test.ts @@ -3,7 +3,7 @@ import prompts from 'prompts'; import { describe, beforeAll, afterAll, afterEach, it, expect } from 'vitest'; import Command from '../../src/commands/login.js'; -import APIError from '../../src/lib/apiError.js'; +import { APIv1Error } from '../../src/lib/apiError.js'; import configStore from '../../src/lib/configstore.js'; import { getAPIV1Mock } from '../helpers/get-api-mock.js'; import { runCommand } from '../helpers/setup-oclif-config.js'; @@ -105,7 +105,7 @@ describe('rdme login', () => { const mock = getAPIV1Mock().post('/api/v1/login', { email, password, project }).reply(401, errorResponse); - await expect(run()).rejects.toStrictEqual(new APIError(errorResponse)); + await expect(run()).rejects.toStrictEqual(new APIv1Error(errorResponse)); mock.done(); }); @@ -147,7 +147,7 @@ describe('rdme login', () => { .post('/api/v1/login', { email, password, project: projectThatIsNotYours }) .reply(404, errorResponse); - await expect(run()).rejects.toStrictEqual(new APIError(errorResponse)); + await expect(run()).rejects.toStrictEqual(new APIv1Error(errorResponse)); mock.done(); }); }); diff --git a/__tests__/commands/openapi/index.test.ts b/__tests__/commands/openapi/index.test.ts index 96f8778cc..acdc902cd 100644 --- a/__tests__/commands/openapi/index.test.ts +++ b/__tests__/commands/openapi/index.test.ts @@ -8,7 +8,7 @@ import prompts from 'prompts'; import { describe, beforeAll, beforeEach, afterEach, it, expect, vi, type MockInstance } from 'vitest'; import Command from '../../../src/commands/openapi/index.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import config from '../../../src/lib/config.js'; import petstoreWeird from '../../__fixtures__/petstore-simple-weird-version.json' with { type: 'json' }; import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; @@ -762,7 +762,7 @@ describe('rdme openapi', () => { '--version', invalidVersion, ]), - ).rejects.toStrictEqual(new APIError(errorObject)); + ).rejects.toStrictEqual(new APIv1Error(errorObject)); return mock.done(); }); @@ -824,7 +824,7 @@ describe('rdme openapi', () => { await expect( run([require.resolve('@readme/oas-examples/3.1/json/petstore.json'), '--key', 'key']), - ).rejects.toStrictEqual(new APIError(errorObject)); + ).rejects.toStrictEqual(new APIv1Error(errorObject)); return mock.done(); }); @@ -874,7 +874,7 @@ describe('rdme openapi', () => { await expect( run(['./__tests__/__fixtures__/swagger-with-invalid-extensions.json', '--key', key, '--version', version]), - ).rejects.toStrictEqual(new APIError(errorObject)); + ).rejects.toStrictEqual(new APIv1Error(errorObject)); mockWithHeader.done(); return mock.done(); @@ -909,7 +909,7 @@ describe('rdme openapi', () => { '--version', version, ]), - ).rejects.toStrictEqual(new APIError(errorObject)); + ).rejects.toStrictEqual(new APIv1Error(errorObject)); putMock.done(); return mock.done(); @@ -932,7 +932,7 @@ describe('rdme openapi', () => { await expect( run(['./__tests__/__fixtures__/swagger-with-invalid-extensions.json', '--key', key, '--version', version]), - ).rejects.toStrictEqual(new APIError(errorObject)); + ).rejects.toStrictEqual(new APIv1Error(errorObject)); return mock.done(); }); @@ -965,7 +965,7 @@ describe('rdme openapi', () => { await expect( run([require.resolve('@readme/oas-examples/2.0/json/petstore.json'), '--key', key, '--version', version]), - ).rejects.toStrictEqual(new APIError(errorObject)); + ).rejects.toStrictEqual(new APIv1Error(errorObject)); mockWithHeader.done(); return mock.done(); diff --git a/__tests__/commands/versions/create.test.ts b/__tests__/commands/versions/create.test.ts index e559f8d37..0969fb031 100644 --- a/__tests__/commands/versions/create.test.ts +++ b/__tests__/commands/versions/create.test.ts @@ -3,7 +3,7 @@ import prompts from 'prompts'; import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/versions/create.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -136,7 +136,7 @@ describe('rdme versions:create', () => { const mockRequest = getAPIV1Mock().post('/api/v1/version').basicAuth({ user: key }).reply(400, errorResponse); - await expect(run(['--key', key, version, '--fork', '0.0.5'])).rejects.toStrictEqual(new APIError(errorResponse)); + await expect(run(['--key', key, version, '--fork', '0.0.5'])).rejects.toStrictEqual(new APIv1Error(errorResponse)); mockRequest.done(); }); diff --git a/__tests__/commands/versions/delete.test.ts b/__tests__/commands/versions/delete.test.ts index 8cb58f778..e11041184 100644 --- a/__tests__/commands/versions/delete.test.ts +++ b/__tests__/commands/versions/delete.test.ts @@ -2,7 +2,7 @@ import nock from 'nock'; import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/versions/delete.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -49,7 +49,7 @@ describe('rdme versions:delete', () => { .basicAuth({ user: key }) .reply(200, { version }); - await expect(run(['--key', key, version])).rejects.toStrictEqual(new APIError(errorResponse)); + await expect(run(['--key', key, version])).rejects.toStrictEqual(new APIv1Error(errorResponse)); mockRequest.done(); }); }); diff --git a/__tests__/commands/versions/update.test.ts b/__tests__/commands/versions/update.test.ts index fca232ce2..5d225df0b 100644 --- a/__tests__/commands/versions/update.test.ts +++ b/__tests__/commands/versions/update.test.ts @@ -3,7 +3,7 @@ import prompts from 'prompts'; import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/versions/update.js'; -import APIError from '../../../src/lib/apiError.js'; +import { APIv1Error } from '../../../src/lib/apiError.js'; import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -348,7 +348,7 @@ describe('rdme versions:update', () => { .basicAuth({ user: key }) .reply(400, errorResponse); - await expect(run(['--key', key, version])).rejects.toStrictEqual(new APIError(errorResponse)); + await expect(run(['--key', key, version])).rejects.toStrictEqual(new APIv1Error(errorResponse)); mockRequest.done(); }); diff --git a/__tests__/lib/apiError.test.ts b/__tests__/lib/apiError.test.ts index 5bc847911..0eb73ef89 100644 --- a/__tests__/lib/apiError.test.ts +++ b/__tests__/lib/apiError.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from 'vitest'; -import APIError from '../../src/lib/apiError.js'; +import { APIv1Error } from '../../src/lib/apiError.js'; const response = { error: 'VERSION_FORK_EMPTY', @@ -16,9 +16,9 @@ const response = { ], }; -describe('APIError', () => { +describe('APIv1Error', () => { it('should handle ReadMe API errors', () => { - const error = new APIError(response); + const error = new APIv1Error(response); expect(error.code).toBe(response.error); expect(error.message).toBe( @@ -27,14 +27,14 @@ describe('APIError', () => { }); it('should handle API errors from a fetch `res` object', () => { - const error = new APIError({ error: response }); + const error = new APIv1Error({ error: response }); expect(error.code).toBe(response.error); }); it('should be able to handle generic non-API errors', () => { const msg = 'i am an generic javascript error'; - const error = new APIError(msg); + const error = new APIv1Error(msg); expect(error.code).toBe(msg); expect(error.message).toBe(msg); diff --git a/src/commands/openapi/index.ts b/src/commands/openapi/index.ts index cecdb83cc..549069e69 100644 --- a/src/commands/openapi/index.ts +++ b/src/commands/openapi/index.ts @@ -148,8 +148,8 @@ export default class OpenAPICommand extends BaseCommand { const error = (res: Response) => { return handleAPIv1Res(res).catch(err => { - // If we receive an APIError, no changes needed! Throw it as is. - if (err.name === 'APIError') { + // If we receive an APIv1Error, no changes needed! Throw it as is. + if (err.name === 'APIv1Error') { throw err; } diff --git a/src/lib/apiError.ts b/src/lib/apiError.ts index c03897b43..3c74ae0c5 100644 --- a/src/lib/apiError.ts +++ b/src/lib/apiError.ts @@ -1,4 +1,4 @@ -export interface APIErrorResponse { +export interface APIv1ErrorResponse { docs?: string; error: string; help?: string; @@ -7,11 +7,11 @@ export interface APIErrorResponse { suggestion?: string; } -export default class APIError extends Error { +export class APIv1Error extends Error { code: string; - constructor(res: APIErrorResponse | string | { error: APIErrorResponse }) { - let err: APIErrorResponse | string; + constructor(res: APIv1ErrorResponse | string | { error: APIv1ErrorResponse }) { + let err: APIv1ErrorResponse | string; // Special handling to for fetch `res` arguments where `res.error` will contain our API error // response. @@ -19,7 +19,7 @@ export default class APIError extends Error { if (typeof res?.error === 'object') { err = res.error; } else { - err = res as APIErrorResponse; + err = res as APIv1ErrorResponse; } } else { err = res; @@ -27,7 +27,7 @@ export default class APIError extends Error { super(err as unknown as string); - this.name = 'APIError'; + this.name = 'APIv1Error'; if (typeof err === 'object') { this.code = err.error; @@ -41,7 +41,7 @@ export default class APIError extends Error { this.message = err.message; } - this.name = 'APIError'; + this.name = 'APIv1Error'; } else { this.code = err; this.message = err; diff --git a/src/lib/readmeAPIFetch.ts b/src/lib/readmeAPIFetch.ts index 99d8f10a3..f02ceb857 100644 --- a/src/lib/readmeAPIFetch.ts +++ b/src/lib/readmeAPIFetch.ts @@ -7,7 +7,7 @@ import { ProxyAgent } from 'undici'; import pkg from '../package.json' with { type: 'json' }; -import APIError from './apiError.js'; +import { APIv1Error } from './apiError.js'; import config from './config.js'; import { git } from './createGHA/index.js'; import isCI, { ciName, isGHA } from './isCI.js'; @@ -212,12 +212,12 @@ export async function readmeAPIv1Fetch( /** * Small handler for handling responses from API v1. * - * If we receive JSON errors, we throw an APIError exception. + * If we receive JSON errors, we throw an APIv1Error exception. * * If we receive non-JSON responses, we consider them errors and throw them. * * @param rejectOnJsonError if omitted (or set to true), the function will return - * an `APIError` if the JSON body contains an `error` property. If set to false, + * an `APIv1Error` if the JSON body contains an `error` property. If set to false, * the function will return a resolved promise containing the JSON object. * */ @@ -230,7 +230,7 @@ export async function handleAPIv1Res(res: Response, rejectOnJsonError = true) { const body = (await res.json()) as any; debug(`received status code ${res.status} from ${res.url} with JSON response: ${JSON.stringify(body)}`); if (body.error && rejectOnJsonError) { - return Promise.reject(new APIError(body)); + return Promise.reject(new APIv1Error(body)); } return body; } diff --git a/src/lib/syncDocsPath.ts b/src/lib/syncDocsPath.ts index 8c80737bc..b3fcfb582 100644 --- a/src/lib/syncDocsPath.ts +++ b/src/lib/syncDocsPath.ts @@ -9,7 +9,7 @@ import path from 'node:path'; import chalk from 'chalk'; import toposort from 'toposort'; -import APIError from './apiError.js'; +import { APIv1Error } from './apiError.js'; import readdirRecursive from './readdirRecursive.js'; import readDoc from './readDoc.js'; import { cleanAPIv1Headers, handleAPIv1Res, readmeAPIv1Fetch } from './readmeAPIFetch.js'; @@ -116,7 +116,7 @@ async function pushDoc( .then(async res => { const body = await handleAPIv1Res(res, false); if (!res.ok) { - if (res.status !== 404) return Promise.reject(new APIError(body)); + if (res.status !== 404) return Promise.reject(new APIv1Error(body)); this.debug(`error retrieving data for ${slug}, creating doc`); return createDoc(); } diff --git a/src/lib/versionSelect.ts b/src/lib/versionSelect.ts index 2516239aa..8326cbfad 100644 --- a/src/lib/versionSelect.ts +++ b/src/lib/versionSelect.ts @@ -1,7 +1,7 @@ -import type { APIErrorResponse } from './apiError.js'; +import type { APIv1ErrorResponse } from './apiError.js'; import type { Version } from '../commands/versions/index.js'; -import APIError from './apiError.js'; +import { APIv1Error } from './apiError.js'; import isCI from './isCI.js'; import { warn } from './logger.js'; import promptTerminal from './promptWrapper.js'; @@ -65,6 +65,6 @@ export async function getProjectVersion( return versionSelection; } catch (err) { - return Promise.reject(new APIError(err as APIErrorResponse)); + return Promise.reject(new APIv1Error(err as APIv1ErrorResponse)); } }