Skip to content

Commit

Permalink
Ensure error objects have consistent property names
Browse files Browse the repository at this point in the history
`message` is used as a high-level description of the errors
`detail` is optional and has an plain language explanation of the
individual errors
`errors` is an array of each individual problem from `detail` in a
machine-readable format
  • Loading branch information
andrewn committed Jul 2, 2019
1 parent 64e39aa commit ba4ead7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
Expand Up @@ -244,7 +244,8 @@ describe('project.controller', () => {
expect(response.json).toHaveBeenCalled();

expect(JSON.parse(JSON.stringify(doc))).toMatchObject({
message: 'Slug "a-slug" is not unique. Check cde123'
message: 'Sketch Validation Failed',
detail: 'Slug "a-slug" is not unique. Check cde123'
});

done();
Expand Down Expand Up @@ -275,7 +276,8 @@ describe('project.controller', () => {
const responseBody = JSON.parse(JSON.stringify(doc));

expect(response.status).toHaveBeenCalledWith(422);
expect(responseBody.name).toBe('File Validation Failed');
expect(responseBody.message).toBe('File Validation Failed');
expect(responseBody.detail).not.toBeNull();
expect(responseBody.errors.length).toBe(1);
expect(responseBody.errors).toEqual([
{ name: 'index.html', message: 'missing \'url\' or \'content\'' }
Expand Down Expand Up @@ -305,8 +307,8 @@ describe('project.controller', () => {
const responseBody = JSON.parse(JSON.stringify(doc));

expect(response.status).toHaveBeenCalledWith(422);
expect(responseBody.name).toBe('File Validation Failed');
expect(responseBody.message).toBe('\'files\' must be an object');
expect(responseBody.message).toBe('File Validation Failed');
expect(responseBody.detail).toBe('\'files\' must be an object');

done();
}
Expand Down
Expand Up @@ -46,7 +46,9 @@ describe('project.controller', () => {

function expectations() {
expect(response.status).toHaveBeenCalledWith(403);
expect(response.json).toHaveBeenCalledWith({ success: false, message: 'Authenticated user does not match owner of project' });
expect(response.json).toHaveBeenCalledWith({
message: 'Authenticated user does not match owner of project'
});

done();
}
Expand All @@ -73,7 +75,9 @@ describe('project.controller', () => {

function expectations() {
expect(response.status).toHaveBeenCalledWith(404);
expect(response.json).toHaveBeenCalledWith({ success: false, message: 'Project with that id does not exist' });
expect(response.json).toHaveBeenCalledWith({
message: 'Project with that id does not exist'
});

done();
}
Expand Down Expand Up @@ -103,7 +107,7 @@ describe('project.controller', () => {

function expectations() {
expect(response.status).toHaveBeenCalledWith(200);
expect(response.json).toHaveBeenCalledWith({ success: true });
expect(response.json).not.toHaveBeenCalled();
expect(deleteObjectsFromS3).toHaveBeenCalled();

done();
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/project.controller/createProject.js
Expand Up @@ -38,15 +38,15 @@ export function apiCreateProject(req, res) {

function sendValidationErrors(err, type, code = 422) {
res.status(code).json({
name: `${type} Validation Failed`,
message: err.message,
message: `${type} Validation Failed`,
detail: err.message,
errors: err.files,
});
}

// TODO: Error handling to match spec
function sendFailure(err) {
res.status(500).json({ success: false });
res.status(500).end();
}

function handleErrors(err) {
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/project.controller/deleteProject.js
Expand Up @@ -21,7 +21,7 @@ function deleteFilesFromS3(files) {

export default function deleteProject(req, res) {
function sendFailure(error) {
res.status(error.code).json({ success: false, message: error.message });
res.status(error.code).json({ message: error.message });
}

function sendProjectNotFound() {
Expand All @@ -47,7 +47,7 @@ export default function deleteProject(req, res) {
return;
}

res.status(200).json({ success: true });
res.status(200).end();
});
}

Expand Down

0 comments on commit ba4ead7

Please sign in to comment.