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

Add more context to 403 response errors #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions src/__specs__/responseStatusMessage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import ResponseStatusMessage from '../responseStatusMessage';

describe('ResponseStatusMessage', () => {
describe('#hasAdvice', () => {
describe('when status code is 403', () => {
it('is true', () => {
// eslint-disable-next-line no-unused-expressions
expect(new ResponseStatusMessage({ code: 403, text: 'Forbidden' }).hasAdvice).to.be.true;
});
});

describe('when status code is not 403', () => {
describe('when status code is 200', () => {
it('is false', () => {
// eslint-disable-next-line no-unused-expressions
expect(new ResponseStatusMessage({ code: 200, text: 'Success' }).hasAdvice).to.be.false;
});
});
});
});

describe('#advice', () => {
describe('when status code is 403', () => {
it('returns advice for recovering from a Forbidden error', () => {
expect(new ResponseStatusMessage({ code: 403, text: 'Forbidden' }).advice).
to.equal('Was a valid API key provided for an organization & application you have access to?');
});
});

describe('when status code is not 403', () => {
describe('when status code is 200', () => {
it('returns undefined', () => {
// eslint-disable-next-line no-unused-expressions
expect(new ResponseStatusMessage({ code: 200, text: 'Success' }).advice).to.be.undefined;
});
});
});
});

describe('#toString', () => {
describe('when status code is 403', () => {
it('returns advice for recovering from a Forbidden error', () => {
expect(new ResponseStatusMessage({ code: 403, text: 'Forbidden' }).toString()).
to.equal('403 Forbidden - Was a valid API key provided for an organization & application you have access to?');
});
});

describe('when status code is not 403', () => {
describe('when status code is 200', () => {
it('returns status code and status text', () => {
expect(new ResponseStatusMessage({ code: 200, text: 'Success' }).toString()).to.equal('200 Success');
});
});
});
});
});
9 changes: 8 additions & 1 deletion src/formatError.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import ResponseStatusMessage from './responseStatusMessage';

export default async function formatError(res, { verbose = false } = {}) {
if (res.status < 300) {
return;
}

console.error(`${res.status} ${res.statusText}`);
console.error(
new ResponseStatusMessage({
code: res.status,
text: res.statusText,
}).toString()
);

const body = await res.text();

Expand Down
28 changes: 28 additions & 0 deletions src/responseStatusMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const RESPONSE_STATUS_CODES_TO_ADVICE = Object.freeze({
403: 'Was a valid API key provided for an organization & application you have access to?',
});

class ResponseStatusMessage {
constructor({ code, text }) {
this.code = code;
this.text = text;
}

get advice() {
return RESPONSE_STATUS_CODES_TO_ADVICE[this.code];
}

get hasAdvice() {
return this.code in RESPONSE_STATUS_CODES_TO_ADVICE;
}

toString() {
const formattedMessage = `${this.code} ${this.text}`;
if (this.hasAdvice) {
return `${formattedMessage} - ${this.advice}`;
}
return formattedMessage;
}
}

export default ResponseStatusMessage;