Skip to content

Commit

Permalink
feat(core/create): adds default InternalServerError and intercepts er…
Browse files Browse the repository at this point in the history
…rors on application
  • Loading branch information
rafamel committed Oct 13, 2019
1 parent 7bf98da commit 819a4b7
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions packages/core/src/create/application/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
CollectionTreeImplementation,
CollectionTreeApplication,
CollectionTree,
TreeTypes,
Expand All @@ -10,9 +9,14 @@ import { traverse, isElementType } from '~/utils';
import camelcase from 'camelcase';
import serviceIntercepts from './service-intercepts';
import { mergeServiceTypes } from './merge';
import { error } from '../types';
import { intercepts, intercept } from '../intercepts';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { PublicError, CollectionError } from '~/errors';

export default function application(
collection: CollectionTree | CollectionTreeImplementation,
collection: CollectionTree,
options?: CreateApplicationOptions
): CollectionTreeApplication {
const opts = Object.assign(
Expand All @@ -24,8 +28,31 @@ export default function application(
options
);

// clone collection
// clone collection and add internal server error
collection = clone(collection);
if (!collection.types.InternalServerError) {
collection.types.InternalServerError = error({ code: 'ServerError' });
}
collection = intercepts(
collection,
[
intercept({
errors: { InternalServerError: 'InternalServerError' },
factory: () => (data, context, next) => {
return next(data).pipe(
catchError((err) =>
throwError(
err instanceof PublicError
? err
: new CollectionError(collection, 'InternalServerError')
)
)
);
}
})
],
{ prepend: true }
);

const types = {
source: collection.types,
Expand Down

0 comments on commit 819a4b7

Please sign in to comment.