Created
October 10, 2012 17:38
errorhandling
This file contains hidden or 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
// Lovely simple-to-read code: | |
function handleDelete(request, response) { | |
getBlogByGuid(request.url.query.guid, function gotBlog(err, blog) { | |
deleteDocumentById(blog.id, function deletedBlog(err, status) { | |
// Deleted OK | |
response.writeHead(200); | |
response.end('Blog Deleted'); | |
}) | |
}); | |
} | |
// Now with some error checking: | |
function handleDelete(request, response) { | |
function reportError(msg, err) { | |
console.log('Blogs', msg); | |
response.json(500, JSON.stringify(err)); | |
} | |
if(! "guid" in request.url.query)) | |
reportError('Error - Missing guid in handleDelete', err) && return; | |
getBlogByGuid(request.url.query.guid, function gotBlog(err, blog) { | |
if(err == true) | |
reportError('Error in getBlogByGuid callback in handleDelete', err) && return; | |
deleteDocumentById(blog.id, function deletedBlog(err, status) { | |
if(err == true) | |
reportError('Error in deleteDocumentById callback in handleDelete', err) && return; | |
// Deleted OK | |
response.writeHead(200); | |
response.end('Blog Deleted'); | |
}) | |
}); | |
} | |
// :( What happened to my lovely readable code? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment