Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/server/routes/api/document/crossref/works.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import logger from '../../../../logger';

import { search, get } from './api.js';
import { toPubMedArticle } from './map';
import { isDoi } from '../../../../../util';
import { HTTPStatusError, isDoi } from '../../../../../util';
import { ArticleIDError } from '../../../../../util/pubmed';

const ID_TYPE = Object.freeze({
Expand Down Expand Up @@ -111,7 +111,19 @@ const findPreprint = async paperId => {
}

} catch( err ) {
logger.error( `${err.name}: ${err.message}` );
if( err instanceof HTTPStatusError ){
const { response } = err;
const { url, headers } = response;
logger.error( `crossref.findPreprint threw ${err.name}: ${err.message}` );
logger.error( `fetch URL: ${url}` );
logger.error( headers.raw() );

} else if( err instanceof ArticleIDError ){
logger.debug( `crossref.findPreprint threw ${err.name}: ${err.message}` );

} else {
logger.error( `crossref.findPreprint threw ${err.name}: ${err.message}` );
}
throw err;
}
};
Expand Down
17 changes: 15 additions & 2 deletions src/server/routes/api/document/pubmed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ArticleIDError, getPubmedCitation } from '../../../../../util/pubmed';
import {
DEMO_ID
} from '../../../../../config';
import { isDigits, isDoi } from '../../../../../util';
import { HTTPStatusError, isDigits, isDoi } from '../../../../../util';

const ID_TYPE = Object.freeze({
DOI: 'doi',
Expand Down Expand Up @@ -93,6 +93,7 @@ const getPubmedArticle = async paperId => {
try {
const IdType = paperId2Type( paperId );
const fieldOpts = IdType === ID_TYPE.TITLE ? { field: ID_TYPE.TITLE } : {};
// TODO - this should fetch when PMID-like, search otherwise
const { searchHits } = await searchPubmed( paperId, fieldOpts );
const PubmedArticle = await findMatchingPubmedArticle( paperId, IdType, searchHits );

Expand All @@ -103,7 +104,19 @@ const getPubmedArticle = async paperId => {
}

} catch( err ) {
logger.error( `${err.name}: ${err.message}` );
if( err instanceof HTTPStatusError ){
const { response } = err;
const { url, headers } = response;
logger.error( `pubmed.getPubmedArticle threw ${err.name}: ${err.message}` );
logger.error( `fetch URL: ${url}` );
logger.error( headers.raw() );

} else if( err instanceof ArticleIDError ){
logger.debug( `pubmed.getPubmedArticle threw ${err.name}: ${err.message}` );

} else {
logger.error( `pubmed.getPubmedArticle threw ${err.name}: ${err.message}` );
}
throw err;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/util/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* Class representing a Fetch Response error
*/
class HTTPStatusError extends Error {
constructor( message, status, statusText ) {
constructor( message, status, statusText, response ) {
super( message );
this.status = status;
this.statusText = statusText;
this.name = 'HTTPStatusError';
this.response = response;
}
}

Expand All @@ -24,7 +25,7 @@ class HTTPStatusError extends Error {
const checkHTTPStatus = response => {
const { statusText, status, ok } = response;
if ( !ok ) {
throw new HTTPStatusError( `${statusText} (${status})`, status, statusText );
throw new HTTPStatusError( `${statusText} (${status})`, status, statusText, response );
}
return response;
};
Expand Down