Skip to content

Commit

Permalink
refactor: remove superflouous facade
Browse files Browse the repository at this point in the history
Refs #397
  • Loading branch information
thewilkybarkid committed Aug 16, 2021
1 parent 1d2811e commit de86db5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/backend/models/preprints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EntityRepository, MikroORM, Repository } from '@mikro-orm/core';
import { Preprint } from './entities';
import { decodePreprintId } from '../../common/utils/ids';
import { ChainError } from '../../common/errors';
import ChainedError from 'typescript-chained-error';
import { getLogger } from '../log.js';

const log = getLogger('backend:model:preprints');
Expand All @@ -18,7 +18,7 @@ export class PreprintModel extends EntityRepository<Preprint> {
try {
return this.findOne({ uuid: value }, params);
} catch (err) {
throw new ChainError(`'${value}' is not a valid UUID or Handle`);
throw new ChainedError(`'${value}' is not a valid UUID or Handle`);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/backend/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EntityRepository, MikroORM, Repository } from '@mikro-orm/core';
import orcidUtils from 'orcid-utils';
import { validate as uuidValidate } from 'uuid';
import { User } from './entities';
import { ChainError } from '../../common/errors';
import ChainedError from 'typescript-chained-error';

@Repository(User)
export class UserModel extends EntityRepository<User> {
Expand All @@ -13,7 +13,7 @@ export class UserModel extends EntityRepository<User> {
}
return this.findOne({ uuid: value }, params);
} catch (err) {
throw new ChainError('Failed to parse ORCID for user.', err);
throw new ChainedError('Failed to parse ORCID for user.', err);
}
}

Expand All @@ -24,7 +24,7 @@ export class UserModel extends EntityRepository<User> {
}
return this.findOne({ personas: { uuid: value } }, params);
} catch (err) {
throw new ChainError(`Failed to find user for persona ${value}.`, err);
throw new ChainedError(`Failed to find user for persona ${value}.`, err);
}
}

Expand All @@ -36,7 +36,7 @@ export class UserModel extends EntityRepository<User> {
params,
);
} catch (err) {
throw new ChainError(`Failed to find user for API appId ${app}.`, err);
throw new ChainedError(`Failed to find user for API appId ${app}.`, err);
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/backend/utils/http-errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ChainedErrorFactory } from 'typescript-chained-error';
import { ChainError } from '../../common/errors';
import ChainedError, { ChainedErrorFactory } from 'typescript-chained-error';

interface HttpErrorProps {
expose?: boolean;
headers?: Record<string, string>;
cleanStack?: boolean;
}

class HttpError extends ChainError {
class HttpError extends ChainedError {
readonly expose: boolean;
readonly headers?: Record<string, string>;
readonly status: number;
Expand All @@ -24,7 +23,7 @@ class HttpError extends ChainError {
}
const cleanStack =
properties && properties.cleanStack ? properties.cleanStack : true;
super(`HTTP Error ${status}: ${message}`, cause, cleanStack);
super(`HTTP Error ${status}: ${message}`, cause, { cleanStack });
this.status = status;
this.expose =
properties && properties.expose ? properties.expose : status < 500;
Expand Down
8 changes: 4 additions & 4 deletions src/backend/utils/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scrape from 'html-metadata';
import { search as scholarSearch } from 'scholarly';
import CrossRef from 'crossref';
import { getOrcidPerson } from './orcid.js';
import { ChainError } from '../../common/errors';
import ChainedError from 'typescript-chained-error';
import { getLogger } from '../log.js';

const log = getLogger('backend:utils:resolve');
Expand Down Expand Up @@ -43,7 +43,7 @@ async function scrapeUrl(
headers: { 'User-Agent': 'webscraper' },
});
} catch (err) {
throw new ChainError('Failed to scrape metadata:', err);
throw new ChainedError('Failed to scrape metadata:', err);
}

if (res) {
Expand Down Expand Up @@ -121,7 +121,7 @@ async function searchGoogleScholar(handle: string): Promise<PreprintMetadata> {
try {
res = await scholarSearch(handle);
} catch (err) {
throw new ChainError('Failed to search Google Scholar:', err);
throw new ChainedError('Failed to search Google Scholar:', err);
}

if (res.length < 1) {
Expand Down Expand Up @@ -159,7 +159,7 @@ function searchCrossRef(handle: string): Promise<PreprintMetadata> {
return new Promise((resolve, reject) => {
CrossRef.work(handle, (err, res) => {
if (err) {
reject(new ChainError('Failed to search CrossRef:', err));
reject(new ChainedError('Failed to search CrossRef:', err));
} else {
if (!res) {
log.error(`Preprint ${handle} not found on CrossRef.`);
Expand Down
7 changes: 0 additions & 7 deletions src/common/errors.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/common/utils/ids.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import doiRegex from 'doi-regex';
import identifiersArxiv from 'identifiers-arxiv';
import { ChainError } from '../errors.ts';
import ChainedError from 'typescript-chained-error';

export function createPreprintId(
value, // doi or arXiv (prefixed or not) or preprint
Expand All @@ -13,7 +13,7 @@ export function createPreprintId(
id = value.doi || value.arXivId;
}
if (!id) {
throw new ChainError('Invalid identifier for create preprint id');
throw new ChainedError('Invalid identifier for create preprint id');
}
}

Expand All @@ -33,7 +33,7 @@ export function createPreprintId(
}

if (!vendor) {
throw new ChainError(
throw new ChainedError(
'Invalid identifier for create preprint id (could not extract vendor)',
);
}
Expand All @@ -43,7 +43,7 @@ export function createPreprintId(

export function decodePreprintId(value) {
if (!value) {
throw new ChainError('You must provide a preprintId to decode');
throw new ChainedError('You must provide a preprintId to decode');
}

let scheme;
Expand All @@ -54,7 +54,7 @@ export function decodePreprintId(value) {
}

if (!scheme) {
throw new ChainError(
throw new ChainedError(
'String is not an encoded preprint ID (could not extract scheme)',
);
}
Expand Down

0 comments on commit de86db5

Please sign in to comment.