diff --git a/datasetxml2oai-pmh.xslt b/datasetxml2oai-pmh.xslt index 42f305d..b7b7b28 100755 --- a/datasetxml2oai-pmh.xslt +++ b/datasetxml2oai-pmh.xslt @@ -360,7 +360,7 @@ - + @@ -406,7 +406,7 @@ doc-type:ResearchData - + diff --git a/src/controllers/oai.controller.ts b/src/controllers/oai.controller.ts index 76e936e..673d295 100644 --- a/src/controllers/oai.controller.ts +++ b/src/controllers/oai.controller.ts @@ -12,7 +12,7 @@ import { Dataset, Project, License } from "../models/init-models"; import Logger from "jet-logger"; import { BadOaiModelException, OaiModelException } from "../exceptions/OaiModelException"; import PageNotFoundException from "../exceptions/PageNotFoundException"; -import { OaiErrorCodes } from "../exceptions/OaiErrorCodes"; +import { OaiErrorCodes, OaiModelError } from "../exceptions/OaiErrorCodes"; import XmlModel from "../library/XmlModel"; import Configuration from "../library/oai/OaiConfiguration"; import ResumptionToken from "../library/oai/ResumptionToken"; @@ -89,7 +89,12 @@ export class OaiController { await this.handleRequest(oaiRequest, request); } catch (error) { if (error instanceof OaiModelException) { - this.xsltParameter["oai_error_code"] = error.oaiCode; + let code = error.oaiCode; + let oaiErrorCode: string | undefined = "Unknown oai error code " + code; + if (OaiModelError.has(error.oaiCode) && OaiModelError.get(code) != undefined) { + oaiErrorCode = OaiModelError.get(error.oaiCode); + } + this.xsltParameter["oai_error_code"] = oaiErrorCode; this.xsltParameter["oai_error_message"] = error.message; } else { // return next(error); // passing to default express middleware error handler @@ -255,6 +260,13 @@ export class OaiController { throw new BadOaiModelException("The prefix of the identifier argument is unknown."); } const dataId = Number(this.getDocumentIdByIdentifier(oaiRequest.identifier)); + if (isNaN(dataId)) { + throw new OaiModelException( + StatusCodes.INTERNAL_SERVER_ERROR, + "The value of the identifier argument is illegal in this repository.", + OaiErrorCodes.BADARGUMENT, + ); + } // let dataset: Dataset | null; @@ -438,6 +450,14 @@ export class OaiController { let fromDate = dayjs(from); const until = oaiRequest["until"] as string; let untilDate = dayjs(until); + if (!fromDate.isValid() || !untilDate.isValid()) { + throw new OaiModelException( + StatusCodes.INTERNAL_SERVER_ERROR, + "Date Parameter is not valid.", + OaiErrorCodes.BADARGUMENT, + ); + } + if (from.length != until.length) { throw new OaiModelException( StatusCodes.INTERNAL_SERVER_ERROR, @@ -460,6 +480,13 @@ export class OaiController { } else if ("from" in oaiRequest && !("until" in oaiRequest)) { const from = oaiRequest["from"] as string; let fromDate = dayjs(from); + if (!fromDate.isValid()) { + throw new OaiModelException( + StatusCodes.INTERNAL_SERVER_ERROR, + "From date parameter is not valid.", + OaiErrorCodes.BADARGUMENT, + ); + } fromDate.hour() == 0 && (fromDate = fromDate.startOf("day")); const now = dayjs(); @@ -480,6 +507,13 @@ export class OaiController { } else if (!("from" in oaiRequest) && "until" in oaiRequest) { const until = oaiRequest["until"] as string; let untilDate = dayjs(until); + if (!untilDate.isValid()) { + throw new OaiModelException( + StatusCodes.INTERNAL_SERVER_ERROR, + "Until date parameter is not valid.", + OaiErrorCodes.BADARGUMENT, + ); + } untilDate.hour() == 0 && (untilDate = untilDate.endOf("day")); const firstPublishedDataset: Dataset = (await Dataset.earliestPublicationDate()) as Dataset; diff --git a/src/exceptions/OaiErrorCodes.ts b/src/exceptions/OaiErrorCodes.ts index 9842566..3cb2fd4 100644 --- a/src/exceptions/OaiErrorCodes.ts +++ b/src/exceptions/OaiErrorCodes.ts @@ -9,3 +9,42 @@ export enum OaiErrorCodes { // 👇️ default export // export { OaiErrorCodes }; + +// https://medium.com/@juliapassynkova/map-your-typescript-enums-e402d406b229 +export const OaiModelError = new Map([ + [OaiErrorCodes.BADARGUMENT, 'badVerb'], + [OaiErrorCodes.BADARGUMENT, 'badArgument'], + [OaiErrorCodes.NORECORDSMATCH, 'noRecordsMatch'], + [OaiErrorCodes.CANNOTDISSEMINATEFORMAT, 'cannotDisseminateFormat'], + [OaiErrorCodes.BADRESUMPTIONTOKEN, 'badResumptionToken'], + [OaiErrorCodes.IDDOESNOTEXIST, 'idDoesNotExist'] + ]); + +// class OaiModelError { +// // const BADVERB = 1010; +// // const BADARGUMENT = 1011; +// // const CANNOTDISSEMINATEFORMAT = 1012; +// // const BADRESUMPTIONTOKEN = 1013; +// // const NORECORDSMATCH = 1014; +// // const IDDOESNOTEXIST = 1015; + + +// protected static $oaiErrorCodes = { +// OaiErrorCodes. 'badVerb', +// BADARGUMENT : 'badArgument', +// NORECORDSMATCH: 'noRecordsMatch', +// CANNOTDISSEMINATEFORMAT: 'cannotDisseminateFormat', +// BADRESUMPTIONTOKEN: 'badResumptionToken', +// IDDOESNOTEXIST: 'idDoesNotExist', +// }; + + + // public static function mapCode($code) + // { + // if (false === array_key_exists($code, self::$oaiErrorCodes)) { + // throw new OaiModelException("Unknown oai error code $code"); + // } + // return self::$oaiErrorCodes[$code]; + // } + +// }