Skip to content

Commit

Permalink
Merge pull request particle-iot#45 from AntonPuko/dev
Browse files Browse the repository at this point in the history
httpError fix & move files
  • Loading branch information
jlkalberer authored Dec 17, 2016
2 parents 5d6fce1 + 1047fec commit bdd9220
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/lib/OAuthModel.js → src/OAuthModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
TokenObject,
User,
UserRepository,
} from '../types';
} from './types';

import ouathClients from '../oauthClients.json';
import ouathClients from './oauthClients.json';

class OauthModel {
_userRepository: UserRepository;
Expand Down
13 changes: 7 additions & 6 deletions src/lib/RouteConfig.js → src/RouteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import type {
Middleware,
NextFunction,
} from 'express';
import type { Settings } from '../types';
import type { Settings } from './types';
import type Controller from './controllers/Controller';
import type HttpError from './HttpError';

import OAuthModel from './OAuthModel';
import OAuthServer from 'express-oauth-server';
import multer from 'multer';
import OAuthModel from './OAuthModel';
import HttpError from './lib/HttpError';

// TODO fix flow errors, come up with better name;
const maybe = (middleware: Middleware, condition: boolean): Middleware =>
Expand Down Expand Up @@ -77,7 +77,7 @@ export default (
allowedUploads
? injectFilesMiddleware.fields(allowedUploads)
: defaultMiddleware,
async (request: $Request, response: $Response) => {
async (request: $Request, response: $Response): Promise<void> => {
const argumentNames = (route.match(/:[\w]*/g) || []).map(
(argumentName: string): string => argumentName.replace(':', ''),
);
Expand Down Expand Up @@ -110,8 +110,9 @@ export default (
response.status(functionResult.status).json(functionResult.data);
}
} catch (error) {
response.status(error.status).json({
error: error.message,
const httpError = new HttpError(error);
response.status(httpError.status).json({
error: httpError.message,
ok: false,
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ import api from './views/api_v1';
import eventsV1 from './views/EventViews001';

// Repositories
import DeviceRepository from './lib/repository/DeviceRepository';
import DeviceRepository from './repository/DeviceRepository';
import {
DeviceAttributeFileRepository,
DeviceKeyFileRepository,
} from 'spark-protocol';

// Routing
import routeConfig from './lib/RouteConfig';
import DevicesController from './lib/controllers/DevicesController';
import ProvisioningController from './lib/controllers/ProvisioningController';
import UsersController from './lib/controllers/UsersController';
import WebhooksController from './lib/controllers/WebhooksController';
import routeConfig from './RouteConfig';
import DevicesController from './controllers/DevicesController';
import ProvisioningController from './controllers/ProvisioningController';
import UsersController from './controllers/UsersController';
import WebhooksController from './controllers/WebhooksController';

export default (settings: Settings, deviceServer: Object): $Application => {
const app = express();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow

import type { $Request, $Response } from 'express';
import type { User } from '../../types';
import type { User } from '../types';
import type { HttpResult } from './types';

export default class Controller {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// @flow

import type { Device, DeviceRepository } from '../../types';
import type { DeviceAPIType } from '../deviceToAPI';
import type { Device, DeviceRepository } from '../types';
import type { DeviceAPIType } from '../lib/deviceToAPI';


import Controller from './Controller';
import HttpError from '../HttpError';
import HttpError from '../lib/HttpError';
import allowUpload from '../decorators/allowUpload';
import httpVerb from '../decorators/httpVerb';
import route from '../decorators/route';
import deviceToAPI from '../deviceToAPI';
import deviceToAPI from '../lib/deviceToAPI';

class DevicesController extends Controller {
_deviceRepository: DeviceRepository;
Expand All @@ -31,7 +31,7 @@ class DevicesController extends Controller {

@httpVerb('post')
@route('/v1/binaries')
compileSources() {
compileSources() { // eslint-disable-line class-methods-use-this
throw new HttpError('not supported in the current server version');
}

Expand Down Expand Up @@ -81,7 +81,8 @@ class DevicesController extends Controller {

return this.ok({ result: varValue });
} catch (error) {
if (error.indexOf && error.indexOf('Variable not found') >= 0) {
const errorMessage = error.message;
if (errorMessage.indexOf('Variable not found') >= 0) {
throw new HttpError('Variable not found', 404);
}
throw error;
Expand All @@ -107,22 +108,18 @@ class DevicesController extends Controller {
}
// TODO not implemented yet
// 2 flash device with known app
try {
if (postBody.app_id) {
this._deviceRepository.flashKnownApp(
deviceID,
postBody.app_id,
);
return this.ok({ id: deviceID, status: 'Update started' });
}
if (postBody.app_id) {
this._deviceRepository.flashKnownApp(
deviceID,
postBody.app_id,
);
return this.ok({ id: deviceID, status: 'Update started' });
}

const file = this.request.files.file[0];
if (file && file.originalname.endsWith('.bin')) {
await this._deviceRepository.flashBinary(deviceID, file);
return this.ok({ id: deviceID, status: 'Update started' });
}
} catch (error) {
throw new HttpError(error.message);
const file = this.request.files.file[0];
if (file && file.originalname.endsWith('.bin')) {
await this._deviceRepository.flashBinary(deviceID, file);
return this.ok({ id: deviceID, status: 'Update started' });
}

throw new HttpError('Did not update device');
Expand All @@ -149,10 +146,11 @@ class DevicesController extends Controller {
);
return this.ok(deviceToAPI(device, result));
} catch (error) {
if (error.indexOf && error.indexOf('Unknown Function') >= 0) {
const errorMessage = error.message;
if (errorMessage.indexOf('Unknown Function') >= 0) {
throw new HttpError('Function not found', 404);
}
throw new HttpError(error.message);
throw error;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// @flow

import Controller from './Controller';

class EventsController extends Controller {

}

export default EventsController;
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @flow

import type { DeviceRepository } from '../../types';
import type { DeviceRepository } from '../types';

import Controller from './Controller';
import httpVerb from '../decorators/httpVerb';
import route from '../decorators/route';
import deviceToAPI from '../deviceToAPI';
import HttpError from '../HttpError';
import deviceToAPI from '../lib/deviceToAPI';
import HttpError from '../lib/HttpError';

class ProvisioningController extends Controller {
_deviceRepository: DeviceRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import type {
UserCredentials,
UserRepository,
} from '../../types';
} from '../types';

import basicAuthParser from 'basic-auth-parser';
import Controller from './Controller';
import HttpError from '../HttpError';
import HttpError from '../lib/HttpError';
import anonymous from '../decorators/anonymous';
import httpVerb from '../decorators/httpVerb';
import route from '../decorators/route';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type {
RequestType,
Webhook,
WebhookMutator,
} from '../../types';
} from '../types';

import Controller from './Controller';
import HttpError from '../HttpError';
import HttpError from '../lib/HttpError';
import httpVerb from '../decorators/httpVerb';
import route from '../decorators/route';

Expand Down
1 change: 0 additions & 1 deletion src/lib/controllers/types.js → src/controllers/types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow


export type HttpResult<TType> = {
data: ?TType,
status: number,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import type { Decorator, Descriptor } from './types';
import type { Decorator, Descriptor, HttpVerb } from './types';
import type Controller from '../controllers/Controller';

/* eslint-disable no-param-reassign */
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 10 additions & 3 deletions src/lib/HttpError.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
class HttpError extends Error {
status: number;

constructor(message: string, status?: number = 400) {
super(message);
this.status = status;
constructor(
error: string | Error | HttpError,
status?: number = 400,
) {
super(error.message || error);
if (typeof error.status === 'number') {
this.status = error.status;
} else {
this.status = status;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import type { DeviceServer } from 'spark-protocol';
import type {
Device,
DeviceAttributes,
DeviceAttributeRepository,
Repository,
} from '../../types';
} from '../types';

import Moniker from 'moniker';
import ursa from 'ursa';
import HttpError from '../HttpError';
import HttpError from '../lib/HttpError';

const NAME_GENERATOR = Moniker.generator([Moniker.adjective, Moniker.noun]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @flow

import type { TokenObject, User, UserCredentials } from '../../types';
import type { TokenObject, User, UserCredentials } from '../types';

import { JSONFileManager, uuid } from 'spark-protocol';
import PasswordHasher from '../PasswordHasher';
import HttpError from '../HttpError';
import PasswordHasher from '../lib/PasswordHasher';
import HttpError from '../lib/HttpError';

class UserFileRepository {
_fileManager: JSONFileManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @flow

import type { Webhook } from '../../types';
import type { Webhook } from '../types';

import { JSONFileManager, uuid } from 'spark-protocol';
import HttpError from '../HttpError';
import HttpError from '../lib/HttpError';

class WebhookFileRepository {
_fileManager: JSONFileManager;
Expand Down
4 changes: 2 additions & 2 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/

import path from 'path';
import WebhookFileRepository from './lib/repository/WebhookFileRepository';
import UsersFileRepository from './lib/repository/UserFileRepository';
import WebhookFileRepository from './repository/WebhookFileRepository';
import UsersFileRepository from './repository/UserFileRepository';

export default {
accessTokenLifetime: 7776000, // 90 days,
Expand Down
4 changes: 2 additions & 2 deletions test/setup/settings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @flow

import path from 'path';
import WebhookFileRepository from '../../src/lib/repository/WebhookFileRepository';
import UsersFileRepository from '../../src/lib/repository/UserFileRepository';
import WebhookFileRepository from '../../src/repository/WebhookFileRepository';
import UsersFileRepository from '../../src/repository/UserFileRepository';
import { DeviceAttributeFileRepository, DeviceKeyFileRepository } from 'spark-protocol';

export default {
Expand Down

0 comments on commit bdd9220

Please sign in to comment.