Skip to content

Commit

Permalink
transferring more errors to new type
Browse files Browse the repository at this point in the history
  • Loading branch information
vsun757 committed Feb 23, 2024
1 parent 8190c41 commit 6b56826
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions app/services/collection-indexes-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const superagent = require('superagent');
const CollectionIndex = require('../models/collection-index-model');
const config = require('../config/config');
const CollectionIndexRepository = require('../repository/collection-index-repository');
const { MissingParameterError, BadlyFormattedParameterError, DuplicateIdError } = require('../exceptions');

class CollectionIndexService {

Expand Down Expand Up @@ -42,7 +43,7 @@ class CollectionIndexService {
async retrieveById(id) {
try {
if (!id) {
const error = new Error(errors.missingParameter);
const error = new Error(this.errors.missingParameter);
error.parameterName = 'id';
throw error;
}
Expand All @@ -52,7 +53,7 @@ class CollectionIndexService {
return collectionIndex; // Note: collectionIndex is null if not found
} catch (err) {
if (err.name === 'CastError') {
const error = new Error(errors.badlyFormattedParameter);
const error = new Error(this.errors.badlyFormattedParameter);
error.parameterName = 'id';
throw error;
} else {
Expand All @@ -78,7 +79,7 @@ class CollectionIndexService {
} catch (err) {
if (err.name === 'MongoServerError' && err.code === 11000) {
// 11000 = Duplicate index
const error = new Error(errors.duplicateId);
const error = new Error(this.errors.duplicateId);
throw error;
} else {
throw err;
Expand All @@ -90,9 +91,7 @@ class CollectionIndexService {
async updateFull(id, data) {
try {
if (!id) {
const error = new Error(errors.missingParameter);
error.parameterName = 'id';
throw error;
throw new MissingParameterError;
}

const collectionIndex = await CollectionIndex.findOne({ "collection_index.id": id });
Expand All @@ -111,13 +110,9 @@ class CollectionIndexService {
return savedCollectionIndex;
} catch (err) {
if (err.name === 'CastError') {
const error = new Error(errors.badlyFormattedParameter);
error.parameterName = 'id';
throw error;
throw new BadlyFormattedParameterError;
} else if (err.name === 'MongoServerError' && err.code === 11000) {
// 11000 = Duplicate index
const error = new Error(errors.duplicateId);
throw error;
throw new DuplicateIdError;
} else {
throw err;
}
Expand All @@ -128,7 +123,7 @@ class CollectionIndexService {
async delete(id) {
try {
if (!id) {
const error = new Error(errors.missingParameter);
const error = new Error(this.errors.missingParameter);
error.parameterName = 'id';
throw error;
}
Expand All @@ -149,18 +144,18 @@ class CollectionIndexService {
async retrieveByUrl(url) {
try {
if (!url) {
const error = new Error(errors.missingParameter);
const error = new Error(this.errors.missingParameter);
throw error;
}

const res = await superagent.get(url).set('Accept', 'application/json');

// Handle different error cases
if (res.notFound) {
const error = new Error(errors.notFound);
const error = new Error(this.errors.notFound);
throw error;
} else if (res.badRequest) {
const error = new Error(errors.badRequest);
const error = new Error(this.errors.badRequest);
throw error;
}

Expand All @@ -169,9 +164,9 @@ class CollectionIndexService {
return collectionIndex;
} catch (err) {
if (err.code === 'ENOTFOUND') {
throw new Error(errors.hostNotFound);
throw new Error(this.errors.hostNotFound);
} else if (err.code === 'ECONNREFUSED') {
throw new Error(errors.connectionRefused);
throw new Error(this.errors.connectionRefused);
} else {
throw err;
}
Expand Down

0 comments on commit 6b56826

Please sign in to comment.