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

WIP: Create a ServerError class. #3429

Closed
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
2 changes: 2 additions & 0 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,6 @@ function addParseCloud() {
global.Parse = Parse;
}

ParseServer.Error = require('./ServerError').default;

export default ParseServer;
19 changes: 11 additions & 8 deletions src/Routers/FilesRouter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import express from 'express';
import BodyParser from 'body-parser';
import * as Middlewares from '../middlewares';
import Parse from 'parse/node';
import Config from '../Config';
import mime from 'mime';
import express from 'express';
import BodyParser from 'body-parser';
import * as Middlewares from '../middlewares';
import Parse from 'parse/node';
import Config from '../Config';
import mime from 'mime';
import ParseServer from '../ParseServer';
Copy link
Contributor Author

@acinader acinader Jan 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a fan of re-formatting the whitespace all the time and i don't even think it looks better or is more readable anyway.....


export class FilesRouter {

Expand Down Expand Up @@ -87,8 +88,10 @@ export class FilesRouter {
res.status(201);
res.set('Location', result.url);
res.json(result);
}).catch(() => {
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Could not store file.'));
}).catch((e) => {
const parseError = new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Could not store file.');
const parseServerError = new ParseServer.Error(e.message, parseError);
next(parseServerError);
});
}

Expand Down
19 changes: 19 additions & 0 deletions src/ServerError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Constructs a new ServerError object with the given message and source.
*
* The message is an error that the consumer of the server API will see,
* while the source message is intended for the server logs to aid a developer
*
* @class ServerError
* @constructor
* @param {String} message A detailed description of the error.
* @param {ParseError} source The
*/
export default class ServerError extends Error {
source;

constructor(message, source) {
super(message);
this.source = source;
}
}
33 changes: 20 additions & 13 deletions src/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import AppCache from './cache';
import log from './logger';
import Parse from 'parse/node';
import auth from './Auth';
import Config from './Config';
import ClientSDK from './ClientSDK';
import AppCache from './cache';
import log from './logger';
import Parse from 'parse/node';
import auth from './Auth';
import Config from './Config';
import ClientSDK from './ClientSDK';
import ParseServer from './ParseServer';

// Checks that the request is authorized for this app and checks user
// auth too.
Expand Down Expand Up @@ -232,13 +233,13 @@ export function allowMethodOverride(req, res, next) {
next();
}

export function handleParseErrors(err, req, res, next) {
// TODO: Add logging as those errors won't make it to the PromiseRouter
if (err instanceof Parse.Error) {
var httpStatus;
export function handleParseErrors(err, req, res) {
if (err instanceof Parse.Error || err instanceof ParseServer.Error) {
const parseError = err instanceof Parse.Error ? err : err.source;
let httpStatus;

// TODO: fill out this mapping
switch (err.code) {
switch (parseError.code) {
case Parse.Error.INTERNAL_SERVER_ERROR:
httpStatus = 500;
break;
Expand All @@ -249,9 +250,11 @@ export function handleParseErrors(err, req, res, next) {
httpStatus = 400;
}

log.error(err.message);
res.status(httpStatus);
res.json({code: err.code, error: err.message});
res.json({code: parseError.code, error: parseError.message});
} else if (err.status && err.message) {
log.error(err.message);
res.status(err.status);
res.json({error: err.message});
} else {
Expand All @@ -260,7 +263,11 @@ export function handleParseErrors(err, req, res, next) {
res.json({code: Parse.Error.INTERNAL_SERVER_ERROR,
message: 'Internal server error.'});
}
next(err);

// this is the end of the road. Don't call next()
// or else the error will go to the default express
// error handler.

}

export function enforceMasterKeyAccess(req, res, next) {
Expand Down