From aca842aad28689f63fd0d715207c6a0a5947adcb Mon Sep 17 00:00:00 2001 From: gdub22 Date: Sat, 13 Sep 2014 09:53:17 -0400 Subject: [PATCH] Better server error messages --- README.md | 5 ++--- server/config.json | 8 ++++---- server/services/embed.js | 20 ++++++++------------ server/services/s3-uploader.js | 15 ++++----------- src/js/content-kit-editor/commands/oembed.js | 2 +- src/js/content-kit-editor/editor/editor.js | 2 +- 6 files changed, 20 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 77a758000..e55698285 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ContentKit-Editor [![Build Status](https://travis-ci.org/ContentKit/content-kit-editor.svg?branch=master)](https://travis-ci.org/ContentKit/content-kit-editor) -A modern, minimalist WYSIWYG editor. (**[Demo](https://rawgit.com/ContentKit/content-kit-editor/master/demo/index.html)**) +A modern, minimalist WYSIWYG editor. ![screenshot] (https://rawgit.com/ContentKit/content-kit-editor/master/screenshot.png) @@ -13,8 +13,7 @@ A modern, minimalist WYSIWYG editor. (**[Demo](https://rawgit.com/ContentKit/co ## Playing 1. Start the server: `npm start` -2. Navigate to the demo at (http://localhost:5000) -``` +2. Navigate to the demo at [http://localhost:5000] ## Testing `gulp test` diff --git a/server/config.json b/server/config.json index 8cc8b42a2..b5498e9f3 100644 --- a/server/config.json +++ b/server/config.json @@ -1,14 +1,14 @@ { "aws" : { - "accessKeyId" : null, - "secretAccessKey" : null, + "accessKeyId" : "XXXXXX", + "secretAccessKey" : "XXXXXX", "region" : "us-east-1" }, "s3" : { - "bucketName" : null, + "bucketName" : "XXXXXX", "maxFileSize" : 5000000 }, - "embedlyKey" : null, + "embedlyKey" : "XXXXXX", "cors" : { "origin" : "*" } diff --git a/server/services/embed.js b/server/services/embed.js index 1fc370b7b..5db988965 100644 --- a/server/services/embed.js +++ b/server/services/embed.js @@ -1,14 +1,12 @@ var embedly = require('embedly'); var util = require('util'); - -var EMBEDLY_KEY = 'XXXXXX'; +var config = require('../config'); module.exports = function(req, res) { - new embedly({key: EMBEDLY_KEY}, function(err, api) { - if (!!err) { - console.error('Error creating Embedly api'); - console.error(err.stack, api); + new embedly({key: config.embedlyKey}, function(err, api) { + if (err) { + console.log(err); return res.status(500).json(err); } @@ -18,14 +16,12 @@ module.exports = function(req, res) { } api.oembed({url: url}, function(err, objs) { - if (!!err) { - console.error(err.stack, objs); - return res.status(500).json(err); + if (err) { + console.log(err, objs); + var message = JSON.parse(objs).error_message; + return res.status(500).json(message); } - if (objs[0].error_message) { - return res.status(500).json(objs[0].error_message); - } console.log(util.inspect(objs[0])); res.json(objs[0]); }); diff --git a/server/services/s3-uploader.js b/server/services/s3-uploader.js index 06de47894..c3d63f87f 100644 --- a/server/services/s3-uploader.js +++ b/server/services/s3-uploader.js @@ -17,7 +17,7 @@ function readFileStream(req, res) { busboy.on('file', function(fieldname, stream, filename, encoding, mimeType) { if (!filename) { stream.resume(); - return res.json(500, { message: 'invalid file', status: 500 }); + return res.status(500).json({ message: 'invalid file', status: 500 }); } req.files = req.files || {}; @@ -27,12 +27,12 @@ function readFileStream(req, res) { }); stream.on('error', function(error) { - return res.json(500, { message: 'error reading file', status: 500 }); + return res.status(500).json({ message: 'error reading file', status: 500 }); }); stream.on('end', function() { if (this.truncated) { - return res.json(500, { message: 'max file size is ' + maxFileSize + ' bytes', status: 500 }); + return res.status(500).json({ message: 'max file size is ' + maxFileSize + ' bytes', status: 500 }); } var finalBuffer = Buffer.concat(this.chunks); var file = req.files[fieldname] = { @@ -68,17 +68,10 @@ function putFileBufferToS3(file, res) { var request = s3.putObject(params, function(error, data) { if (error) { - return res.json(500, { message: 'error uploading to S3', status: 500 }); + return res.status(500).json({ message: error.message || 'Error uploading to S3', status: 500 }); } res.json({ url: 'https://s3.amazonaws.com/' + uploadBucket + '/' + key }); }); - - // Can we send this to the client? - /* - request.on('httpUploadProgress', function (progress) { - console.log('Uploaded', progress.loaded, 'of', progress.total, 'bytes'); - }); - */ } module.exports = readFileStream; diff --git a/src/js/content-kit-editor/commands/oembed.js b/src/js/content-kit-editor/commands/oembed.js index a5960e293..b759ad335 100644 --- a/src/js/content-kit-editor/commands/oembed.js +++ b/src/js/content-kit-editor/commands/oembed.js @@ -46,7 +46,7 @@ OEmbedCommand.prototype.exec = function(url) { if (error.target && error.target.status === 0) { errorMsg = 'Error: could not connect to embed service.'; } else if (typeof error !== 'string') { - errorMsg = 'Error: embeds are broken :/'; + errorMsg = 'Error: unexpected embed error.'; } new Message().show(errorMsg); embedIntent.show(); diff --git a/src/js/content-kit-editor/editor/editor.js b/src/js/content-kit-editor/editor/editor.js index 09dead56c..e265a6c75 100644 --- a/src/js/content-kit-editor/editor/editor.js +++ b/src/js/content-kit-editor/editor/editor.js @@ -36,7 +36,7 @@ var defaults = { new SubheadingCommand() ], embedCommands: [ - new ImageCommand({ serviceUrl: '/images' }), + new ImageCommand({ serviceUrl: '/upload' }), new OEmbedCommand({ serviceUrl: '/embed' }) ], autoTypingCommands: [