Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dinevillar committed Feb 11, 2018
1 parent fb9a382 commit 8bdd201
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adonis-json-api-serializer",
"version": "0.1.0",
"version": "0.3.1-alpha",
"description": "AdonisJS Agnostic JSON Rest Api Specification Wrapper",
"main": "index.js",
"scripts": {},
Expand Down
2 changes: 0 additions & 2 deletions providers/JsonApiProvider.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class JsonApiProvider extends ServiceProvider {
const JsonApiSpecification = require('../src/Middleware/JsonApiSpecification');
return new JsonApiSpecification(app.use('Adonis/Src/Config'));
});

this.app.bind('JsonApi/Middleware/JsonApiDestroy', () => require('../src/Middleware/JsonApiDestroy'));
}

register() {
Expand Down
121 changes: 67 additions & 54 deletions src/Exceptions/JsonApiException.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,82 @@
const {HttpException} = require('node-exceptions');

class JsonApiException extends HttpException {
constructor(title, status, code, detail, source, id, links, meta) {
super(title, status, code);
constructor(title, status, code, detail, source, id, links, meta) {
super(title, status, code);

Object.defineProperty(this, 'title', {
configurable: true,
enumerable: false,
value: title,
writable: true
});
Object.defineProperty(this, 'title', {
configurable: true,
enumerable: false,
value: title,
writable: true
});

Object.defineProperty(this, 'detail', {
configurable: true,
enumerable: false,
value: detail,
writable: true
});
Object.defineProperty(this, 'detail', {
configurable: true,
enumerable: false,
value: detail,
writable: true
});

Object.defineProperty(this, 'status', {
configurable: true,
enumerable: false,
value: status || 500,
writable: true
});
Object.defineProperty(this, 'status', {
configurable: true,
enumerable: false,
value: status || 500,
writable: true
});

Object.defineProperty(this, 'source', {
configurable: true,
enumerable: false,
value: source || {pointer: ""},
writable: true
});
Object.defineProperty(this, 'source', {
configurable: true,
enumerable: false,
value: source || {pointer: ""},
writable: true
});

Object.defineProperty(this, 'code', {
configurable: true,
enumerable: false,
value: code || "E_JSON_API",
writable: true
});
Object.defineProperty(this, 'code', {
configurable: true,
enumerable: false,
value: code || "E_JSON_API",
writable: true
});

Object.defineProperty(this, 'id', {
configurable: true,
enumerable: false,
value: id,
writable: true
});
Object.defineProperty(this, 'id', {
configurable: true,
enumerable: false,
value: id,
writable: true
});

Object.defineProperty(this, 'links', {
configurable: true,
enumerable: false,
value: links,
writable: true
});
Object.defineProperty(this, 'links', {
configurable: true,
enumerable: false,
value: links,
writable: true
});

Object.defineProperty(this, 'meta', {
configurable: true,
enumerable: false,
value: meta,
writable: true
});
}
Object.defineProperty(this, 'meta', {
configurable: true,
enumerable: false,
value: meta,
writable: true
});
}

static invokeFromError(error) {
return new this(error.name, error.status, error.code, error.message);
}

static invokeFromError(error) {
return new this(error.name, error.status, error.code, error.message);
toJSON() {
return {
title: this.title,
detail: this.detail,
status: this.status,
source: this.source,
code: this.code,
id: this.id,
links: this.links,
meta: this.meta,
}
}
}

module.exports = JsonApiException;
module.exports = JsonApiException;
Empty file modified src/Exceptions/RegistryException.js
100644 → 100755
Empty file.
Empty file modified src/Exceptions/TypeNotDefinedException.js
100644 → 100755
Empty file.
Empty file modified src/Exceptions/index.js
100644 → 100755
Empty file.
Empty file modified src/Exceptions/specification-exceptions.js
100644 → 100755
Empty file.
14 changes: 0 additions & 14 deletions src/Middleware/JsonApiDestroy.js

This file was deleted.

11 changes: 8 additions & 3 deletions src/Middleware/JsonApiSpecification.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const {JsonApiSpecificationException} = require('../Exceptions');
const {JsonApiSerializer} = use('JsonApi');
const JsonApi = use('JsonApi');
const Logger = use('Logger');

class JsonApiSpecification {
constructor(Config) {
Expand Down Expand Up @@ -33,11 +34,15 @@ class JsonApiSpecification {
}
if (doResourceObject) {
if (!request.input('data') || !request.input('data').hasOwnProperty('type')) {
throw JsonApiSpecificationException.UnprocessableResourceObject.invoke()
const uro = JsonApiSpecificationException.UnprocessableResourceObject.invoke()
uro.links = {
about: "http://jsonapi.org/format/#crud"
}
throw uro;
}
const data = request.input('data');
try {
request.body = JsonApiSerializer.deserialize(data.type, {data: data});
request.body = JsonApi.JsonApiSerializer.deserialize(data.type, {data: data});
} catch (error) {
throw JsonApiSpecificationException.UnknownResourceObjectType.invoke(data.type, error.message);
}
Expand Down
Empty file modified src/Serializer/LucidSerializer.js
100644 → 100755
Empty file.
16 changes: 14 additions & 2 deletions src/Service/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const process = require('process');
const Serializer = require('json-api-serializer');
const {JsonApiException} = require('../Exceptions');
const Logger = use('Logger');

class JsonApi {
constructor(Config) {
Expand All @@ -13,16 +14,27 @@ class JsonApi {
this.jsonApiErrors = [];
}

handleError(error) {
parseError(error) {
let jsonApiError = error;
if (!(error instanceof JsonApiException)) {
jsonApiError = JsonApiException.invokeFromError(error);
}
if (this.includeStackTrace) {
jsonApiError.meta = {
stack: error.stack
stack: error.stack.split(/\r?\n/)
}
}
return jsonApiError.toJSON();
}

async handleError(error, {request, response}) {
const jsonApiError = this.parseError(error);
this.pushError(error);
await response.status(jsonApiError.status).send({"errors": this.jsonApiErrors});
this.jsonApiErrors = [];
}

pushError(jsonApiError) {
this.jsonApiErrors.push(jsonApiError);
}

Expand Down

0 comments on commit 8bdd201

Please sign in to comment.