generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nick Cipollo
committed
Dec 12, 2019
1 parent
c4fce56
commit dba5ad5
Showing
5 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ErrorMessage } from "../src/ErrorMessage" | ||
|
||
describe('ErrorMessage', () => { | ||
it('generates message with errors', () => { | ||
const resource = "release" | ||
const error = { | ||
message: 'something bad happened', | ||
errors: [ | ||
{ | ||
code: 'missing', | ||
resource: 'release' | ||
}, | ||
{ | ||
code: 'already_exists', | ||
resource: 'release' | ||
} | ||
] | ||
} | ||
|
||
const errorMessage = new ErrorMessage(error) | ||
|
||
const expectedString = "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' | ||
} | ||
|
||
const errorMessage = new ErrorMessage(error) | ||
|
||
expect(errorMessage.toString()).toBe('something bad happened') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { GithubError } from "../src/GithubError" | ||
|
||
describe('GithubError', () => { | ||
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 | ||
} | ||
|
||
const githubError = new GithubError(error) | ||
const message = githubError.toString() | ||
|
||
expect(message).toBe(`foo\nPlease see ${url}.`) | ||
}) | ||
|
||
it('without documentation url', () => { | ||
const error = { | ||
code: "custom", | ||
message: "foo" | ||
} | ||
|
||
const githubError = new GithubError(error) | ||
const message = githubError.toString() | ||
|
||
expect(message).toBe('foo') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { GithubError } from "./GithubError" | ||
|
||
export class ErrorMessage { | ||
error: any | ||
|
||
constructor(error: any) { | ||
this.error = error | ||
} | ||
|
||
toString(): string { | ||
const message = this.error.message | ||
const errors = this.githubErrors() | ||
if (errors.length > 0) { | ||
return `${message}\nErrors:\n${this.errorBulletedList(errors)}` | ||
} else { | ||
return message | ||
} | ||
} | ||
|
||
githubErrors(): GithubError[] { | ||
const errors = this.error.errors | ||
if (errors instanceof Array) { | ||
return errors.map((err) => new GithubError(err)) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
errorBulletedList(errors: GithubError[]): string { | ||
return errors.map((err) => `- ${err}`).join("\n") | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
export class GithubError { | ||
error: any; | ||
|
||
constructor(error: any) { | ||
this.error = error | ||
} | ||
|
||
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.` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters