Cleanup error classes and CI

This commit is contained in:
Nick Cipollo
2021-03-23 16:15:29 -04:00
parent 9b14e2e2d3
commit 8f0b206fd3
17 changed files with 386 additions and 370 deletions

28
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,28 @@
name: "PR Checks"
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
check_pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: "yarn install"
run: yarn install
- name: "yarn build"
run: yarn build
- name: "check for uncommitted changes"
# Ensure no changes, but ignore node_modules dir since dev/fresh ci deps installed.
run: |
git diff --exit-code --stat -- . ':!node_modules' \
|| (echo "##[error] found changed files after build. please 'yarn build && npm run format'" \
"and check in all changes" \
&& exit 1)

View File

@@ -1,25 +0,0 @@
name: "PR Checks"
on: [pull_request, push]
jobs:
check_pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: "yarn install"
run: yarn install
- name: "yarn build"
run: yarn build
- name: "yarn test"
run: yarn test
- name: "check for uncommitted changes"
# Ensure no changes, but ignore node_modules dir since dev/fresh ci deps installed.
run: |
git diff --exit-code --stat -- . ':!node_modules' \
|| (echo "##[error] found changed files after build. please 'yarn build && npm run format'" \
"and check in all changes" \
&& exit 1)

14
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,14 @@
name: "Test"
on: [push, pull_request]
jobs:
check_pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: "yarn install"
run: yarn install
- name: "yarn test"
run: yarn test

View File

@@ -1,70 +0,0 @@
import { ErrorMessage } from "../src/ErrorMessage"
describe('ErrorMessage', () => {
describe('has error with code', () => {
const error = {
message: 'something bad happened',
errors: [
{
code: 'missing',
resource: 'release'
},
{
code: 'already_exists',
resource: 'release'
}
],
status: 422
}
it('does not have error', () => {
const errorMessage = new ErrorMessage(error)
expect(errorMessage.hasErrorWithCode('missing_field')).toBeFalsy()
})
it('has error', () => {
const errorMessage = new ErrorMessage(error)
expect(errorMessage.hasErrorWithCode('missing')).toBeTruthy()
})
})
it('generates message with errors', () => {
const error = {
message: 'something bad happened',
errors: [
{
code: 'missing',
resource: 'release'
},
{
code: 'already_exists',
resource: 'release'
}
],
status: 422
}
const errorMessage = new ErrorMessage(error)
const expectedString = "Error 422: something bad happened\nErrors:\n- release does not exist.\n- release already exists."
expect(errorMessage.toString()).toBe(expectedString)
})
it('generates message without errors', () => {
const error = {
message: 'something bad happened',
status: 422
}
const errorMessage = new ErrorMessage(error)
expect(errorMessage.toString()).toBe('Error 422: something bad happened')
})
it('provides error status', () => {
const error = { status: 404 }
const errorMessage = new ErrorMessage(error)
expect(errorMessage.status).toBe(404)
})
})

View File

@@ -1,99 +1,70 @@
import { GithubError } from "../src/GithubError"
describe('GithubError', () => {
describe('ErrorMessage', () => {
it('provides error code', () => {
describe('has error with code', () => {
const error = {
code: "missing"
message: 'something bad happened',
errors: [
{
code: 'missing',
resource: 'release'
},
{
code: 'already_exists',
resource: 'release'
}
],
status: 422
}
const githubError = new GithubError(error)
expect(githubError.code).toBe('missing')
})
it('generates missing resource error message', () => {
const resource = "release"
const error = {
code: "missing",
resource: resource
}
const githubError = new GithubError(error)
const message = githubError.toString()
expect(message).toBe(`${resource} does not exist.`)
})
it('generates missing field error message', () => {
const resource = "release"
const field = "body"
const error = {
code: "missing_field",
field: field,
resource: resource
}
const githubError = new GithubError(error)
const message = githubError.toString()
expect(message).toBe(`The ${field} field on ${resource} is missing.`)
})
it('generates invalid field error message', () => {
const resource = "release"
const field = "body"
const error = {
code: "invalid",
field: field,
resource: resource
}
const githubError = new GithubError(error)
const message = githubError.toString()
expect(message).toBe(`The ${field} field on ${resource} is an invalid format.`)
})
it('generates resource already exists error message', () => {
const resource = "release"
const field = "body"
const error = {
code: "already_exists",
resource: resource
}
const githubError = new GithubError(error)
const message = githubError.toString()
expect(message).toBe(`${resource} already exists.`)
})
describe('generates custom error message', () => {
it('with documentation url', () => {
const url = "https://api.example.com"
const error = {
code: "custom",
message: "foo",
documentation_url: url
}
it('does not have error', () => {
const githubError = new GithubError(error)
const message = githubError.toString()
expect(message).toBe(`foo\nPlease see ${url}.`)
expect(githubError.hasErrorWithCode('missing_field')).toBeFalsy()
})
it('without documentation url', () => {
const error = {
code: "custom",
message: "foo"
}
it('has error', () => {
const githubError = new GithubError(error)
const message = githubError.toString()
expect(message).toBe('foo')
expect(githubError.hasErrorWithCode('missing')).toBeTruthy()
})
})
})
it('generates message with errors', () => {
const error = {
message: 'something bad happened',
errors: [
{
code: 'missing',
resource: 'release'
},
{
code: 'already_exists',
resource: 'release'
}
],
status: 422
}
const githubError = new GithubError(error)
const expectedString = "Error 422: something bad happened\nErrors:\n- release does not exist.\n- release already exists."
expect(githubError.toString()).toBe(expectedString)
})
it('generates message without errors', () => {
const error = {
message: 'something bad happened',
status: 422
}
const githubError = new GithubError(error)
expect(githubError.toString()).toBe('Error 422: something bad happened')
})
it('provides error status', () => {
const error = { status: 404 }
const githubError = new GithubError(error)
expect(githubError.status).toBe(404)
})
})

View File

@@ -0,0 +1,98 @@
import { GithubErrorDetail } from "../src/GithubErrorDetail"
describe('GithubErrorDetail', () => {
it('provides error code', () => {
const error = {
code: "missing"
}
const detail = new GithubErrorDetail(error)
expect(detail.code).toBe('missing')
})
it('generates missing resource error message', () => {
const resource = "release"
const error = {
code: "missing",
resource: resource
}
const detail = new GithubErrorDetail(error)
const message = detail.toString()
expect(message).toBe(`${resource} does not exist.`)
})
it('generates missing field error message', () => {
const resource = "release"
const field = "body"
const error = {
code: "missing_field",
field: field,
resource: resource
}
const detail = new GithubErrorDetail(error)
const message = detail.toString()
expect(message).toBe(`The ${field} field on ${resource} is missing.`)
})
it('generates invalid field error message', () => {
const resource = "release"
const field = "body"
const error = {
code: "invalid",
field: field,
resource: resource
}
const detail = new GithubErrorDetail(error)
const message = detail.toString()
expect(message).toBe(`The ${field} field on ${resource} is an invalid format.`)
})
it('generates resource already exists error message', () => {
const resource = "release"
const error = {
code: "already_exists",
resource: resource
}
const detail = new GithubErrorDetail(error)
const message = detail.toString()
expect(message).toBe(`${resource} already exists.`)
})
describe('generates custom error message', () => {
it('with documentation url', () => {
const url = "https://api.example.com"
const error = {
code: "custom",
message: "foo",
documentation_url: url
}
const detail = new GithubErrorDetail(error)
const message = detail.toString()
expect(message).toBe(`foo\nPlease see ${url}.`)
})
it('without documentation url', () => {
const error = {
code: "custom",
message: "foo"
}
const detail = new GithubErrorDetail(error)
const message = detail.toString()
expect(message).toBe('foo')
})
})
})

View File

@@ -20,8 +20,8 @@ describe.skip('Integration Test', () => {
const releases = new GithubReleases(inputs, git)
const uploader = new GithubArtifactUploader(
releases,
inputs.replacesArtifacts,
inputs.artifactErrorsFailBuild,
inputs.replacesArtifacts
)
action = new Action(inputs, releases, uploader)
})

View File

@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Action = void 0;
const ErrorMessage_1 = require("./ErrorMessage");
const GithubError_1 = require("./GithubError");
class Action {
constructor(inputs, releases, uploader) {
this.inputs = inputs;
@@ -55,8 +55,8 @@ class Action {
});
}
static noPublishedRelease(error) {
const errorMessage = new ErrorMessage_1.ErrorMessage(error);
return errorMessage.status == 404;
const githubError = new GithubError_1.GithubError(error);
return githubError.status == 404;
}
updateDraftOrCreateRelease() {
return __awaiter(this, void 0, void 0, function* () {

View File

@@ -1,40 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorMessage = void 0;
const GithubError_1 = require("./GithubError");
class ErrorMessage {
constructor(error) {
this.error = error;
this.githubErrors = this.generateGithubErrors();
}
generateGithubErrors() {
const errors = this.error.errors;
if (errors instanceof Array) {
return errors.map((err) => new GithubError_1.GithubError(err));
}
else {
return [];
}
}
get status() {
return this.error.status;
}
hasErrorWithCode(code) {
return this.githubErrors.some((err) => err.code == code);
}
toString() {
const message = this.error.message;
const errors = this.githubErrors;
const status = this.status;
if (errors.length > 0) {
return `Error ${status}: ${message}\nErrors:\n${this.errorBulletedList(errors)}`;
}
else {
return `Error ${status}: ${message}`;
}
}
errorBulletedList(errors) {
return errors.map((err) => `- ${err}`).join("\n");
}
}
exports.ErrorMessage = ErrorMessage;

View File

@@ -1,57 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubError = void 0;
const GithubErrorDetail_1 = require("./GithubErrorDetail");
class GithubError {
constructor(error) {
this.error = error;
this.githubErrors = this.generateGithubErrors();
}
get code() {
return this.error.code;
}
toString() {
const code = this.error.code;
switch (code) {
case 'missing':
return this.missingResourceMessage();
case 'missing_field':
return this.missingFieldMessage();
case 'invalid':
return this.invalidFieldMessage();
case 'already_exists':
return this.resourceAlreadyExists();
default:
return this.customErrorMessage();
}
}
customErrorMessage() {
const message = this.error.message;
const documentation = this.error.documentation_url;
let documentationMessage;
if (documentation) {
documentationMessage = `\nPlease see ${documentation}.`;
generateGithubErrors() {
const errors = this.error.errors;
if (errors instanceof Array) {
return errors.map((err) => new GithubErrorDetail_1.GithubErrorDetail(err));
}
else {
documentationMessage = "";
return [];
}
return `${message}${documentationMessage}`;
}
invalidFieldMessage() {
const resource = this.error.resource;
const field = this.error.field;
return `The ${field} field on ${resource} is an invalid format.`;
get status() {
return this.error.status;
}
missingResourceMessage() {
const resource = this.error.resource;
return `${resource} does not exist.`;
hasErrorWithCode(code) {
return this.githubErrors.some((err) => err.code == code);
}
missingFieldMessage() {
const resource = this.error.resource;
const field = this.error.field;
return `The ${field} field on ${resource} is missing.`;
toString() {
const message = this.error.message;
const errors = this.githubErrors;
const status = this.status;
if (errors.length > 0) {
return `Error ${status}: ${message}\nErrors:\n${this.errorBulletedList(errors)}`;
}
else {
return `Error ${status}: ${message}`;
}
}
resourceAlreadyExists() {
const resource = this.error.resource;
return `${resource} already exists.`;
errorBulletedList(errors) {
return errors.map((err) => `- ${err}`).join("\n");
}
}
exports.GithubError = GithubError;

57
lib/GithubErrorDetail.js Normal file
View File

@@ -0,0 +1,57 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubErrorDetail = void 0;
class GithubErrorDetail {
constructor(error) {
this.error = error;
}
get code() {
return this.error.code;
}
toString() {
const code = this.error.code;
switch (code) {
case 'missing':
return this.missingResourceMessage();
case 'missing_field':
return this.missingFieldMessage();
case 'invalid':
return this.invalidFieldMessage();
case 'already_exists':
return this.resourceAlreadyExists();
default:
return this.customErrorMessage();
}
}
customErrorMessage() {
const message = this.error.message;
const documentation = this.error.documentation_url;
let documentationMessage;
if (documentation) {
documentationMessage = `\nPlease see ${documentation}.`;
}
else {
documentationMessage = "";
}
return `${message}${documentationMessage}`;
}
invalidFieldMessage() {
const resource = this.error.resource;
const field = this.error.field;
return `The ${field} field on ${resource} is an invalid format.`;
}
missingResourceMessage() {
const resource = this.error.resource;
return `${resource} does not exist.`;
}
missingFieldMessage() {
const resource = this.error.resource;
const field = this.error.field;
return `The ${field} field on ${resource} is missing.`;
}
resourceAlreadyExists() {
const resource = this.error.resource;
return `${resource} already exists.`;
}
}
exports.GithubErrorDetail = GithubErrorDetail;

View File

@@ -35,7 +35,7 @@ const Releases_1 = require("./Releases");
const Action_1 = require("./Action");
const ArtifactUploader_1 = require("./ArtifactUploader");
const ArtifactGlobber_1 = require("./ArtifactGlobber");
const ErrorMessage_1 = require("./ErrorMessage");
const GithubError_1 = require("./GithubError");
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
@@ -43,8 +43,8 @@ function run() {
yield action.perform();
}
catch (error) {
const errorMessage = new ErrorMessage_1.ErrorMessage(error);
core.setFailed(errorMessage.toString());
const githubError = new GithubError_1.GithubError(error);
core.setFailed(githubError.toString());
}
});
}
@@ -55,7 +55,7 @@ function createAction() {
const globber = new ArtifactGlobber_1.FileArtifactGlobber();
const inputs = new Inputs_1.CoreInputs(globber, context);
const releases = new Releases_1.GithubReleases(inputs, git);
const uploader = new ArtifactUploader_1.GithubArtifactUploader(releases, inputs.replacesArtifacts);
const uploader = new ArtifactUploader_1.GithubArtifactUploader(releases, inputs.replacesArtifacts, inputs.artifactErrorsFailBuild);
return new Action_1.Action(inputs, releases, uploader);
}
run();

View File

@@ -1,7 +1,7 @@
import {Inputs} from "./Inputs";
import {CreateReleaseResponse, Releases, UpdateReleaseResponse} from "./Releases";
import {ArtifactUploader} from "./ArtifactUploader";
import {ErrorMessage} from "./ErrorMessage";
import {GithubError} from "./GithubError";
export class Action {
private inputs: Inputs
@@ -55,8 +55,8 @@ export class Action {
}
private static noPublishedRelease(error: any): boolean {
const errorMessage = new ErrorMessage(error)
return errorMessage.status == 404
const githubError = new GithubError(error)
return githubError.status == 404
}
private async updateDraftOrCreateRelease(): Promise<CreateReleaseResponse | UpdateReleaseResponse> {

View File

@@ -1,44 +0,0 @@
import {GithubError} from "./GithubError"
export class ErrorMessage {
private error: any
private githubErrors: GithubError[]
constructor(error: any) {
this.error = error
this.githubErrors = this.generateGithubErrors()
}
private generateGithubErrors(): GithubError[] {
const errors = this.error.errors
if (errors instanceof Array) {
return errors.map((err) => new GithubError(err))
} else {
return []
}
}
get status(): number {
return this.error.status
}
hasErrorWithCode(code: String): boolean {
return this.githubErrors.some((err) => err.code == code)
}
toString(): string {
const message = this.error.message
const errors = this.githubErrors
const status = this.status
if (errors.length > 0) {
return `Error ${status}: ${message}\nErrors:\n${this.errorBulletedList(errors)}`
} else {
return `Error ${status}: ${message}`
}
}
private errorBulletedList(errors: GithubError[]): string {
return errors.map((err) => `- ${err}`).join("\n")
}
}

View File

@@ -1,65 +1,44 @@
import {GithubErrorDetail} from "./GithubErrorDetail"
export class GithubError {
private error: any;
private error: any
private readonly githubErrors: GithubErrorDetail[]
constructor(error: any) {
this.error = error
this.githubErrors = this.generateGithubErrors()
}
get code(): string {
return this.error.code
private generateGithubErrors(): GithubErrorDetail[] {
const errors = this.error.errors
if (errors instanceof Array) {
return errors.map((err) => new GithubErrorDetail(err))
} else {
return []
}
}
get status(): number {
return this.error.status
}
hasErrorWithCode(code: String): boolean {
return this.githubErrors.some((err) => err.code == code)
}
toString(): string {
const code = this.error.code
switch (code) {
case 'missing':
return this.missingResourceMessage()
case 'missing_field':
return this.missingFieldMessage()
case 'invalid':
return this.invalidFieldMessage()
case 'already_exists':
return this.resourceAlreadyExists()
default:
return this.customErrorMessage()
}
}
private customErrorMessage(): string {
const message = this.error.message;
const documentation = this.error.documentation_url
let documentationMessage: string
if (documentation) {
documentationMessage = `\nPlease see ${documentation}.`
const message = this.error.message
const errors = this.githubErrors
const status = this.status
if (errors.length > 0) {
return `Error ${status}: ${message}\nErrors:\n${this.errorBulletedList(errors)}`
} else {
documentationMessage = ""
return `Error ${status}: ${message}`
}
return `${message}${documentationMessage}`
}
private invalidFieldMessage(): string {
const resource = this.error.resource
const field = this.error.field
return `The ${field} field on ${resource} is an invalid format.`
}
private missingResourceMessage(): string {
const resource = this.error.resource
return `${resource} does not exist.`
}
private missingFieldMessage(): string {
const resource = this.error.resource
const field = this.error.field
return `The ${field} field on ${resource} is missing.`
}
private resourceAlreadyExists(): string {
const resource = this.error.resource
return `${resource} already exists.`
private errorBulletedList(errors: GithubErrorDetail[]): string {
return errors.map((err) => `- ${err}`).join("\n")
}
}

65
src/GithubErrorDetail.ts Normal file
View File

@@ -0,0 +1,65 @@
export class GithubErrorDetail {
private error: any;
constructor(error: any) {
this.error = error
}
get code(): string {
return this.error.code
}
toString(): string {
const code = this.error.code
switch (code) {
case 'missing':
return this.missingResourceMessage()
case 'missing_field':
return this.missingFieldMessage()
case 'invalid':
return this.invalidFieldMessage()
case 'already_exists':
return this.resourceAlreadyExists()
default:
return this.customErrorMessage()
}
}
private customErrorMessage(): string {
const message = this.error.message;
const documentation = this.error.documentation_url
let documentationMessage: string
if (documentation) {
documentationMessage = `\nPlease see ${documentation}.`
} else {
documentationMessage = ""
}
return `${message}${documentationMessage}`
}
private invalidFieldMessage(): string {
const resource = this.error.resource
const field = this.error.field
return `The ${field} field on ${resource} is an invalid format.`
}
private missingResourceMessage(): string {
const resource = this.error.resource
return `${resource} does not exist.`
}
private missingFieldMessage(): string {
const resource = this.error.resource
const field = this.error.field
return `The ${field} field on ${resource} is missing.`
}
private resourceAlreadyExists(): string {
const resource = this.error.resource
return `${resource} already exists.`
}
}

View File

@@ -5,15 +5,15 @@ import { GithubReleases } from './Releases';
import { Action } from './Action';
import { GithubArtifactUploader } from './ArtifactUploader';
import { FileArtifactGlobber } from './ArtifactGlobber';
import { ErrorMessage } from './ErrorMessage';
import { GithubError } from './GithubError';
async function run() {
try {
const action = createAction()
await action.perform()
} catch (error) {
const errorMessage = new ErrorMessage(error)
core.setFailed(errorMessage.toString());
const githubError = new GithubError(error)
core.setFailed(githubError.toString());
}
}