- correct oai error code messages needed by validator

This commit is contained in:
Arno Kaimbacher 2022-11-28 10:23:54 +01:00
parent 86d7b064d1
commit 281c321104
3 changed files with 77 additions and 4 deletions

View File

@ -360,7 +360,7 @@
<xsl:template match="SetSpec">
<setSpec>
<xsl:value-of select="@Value" />
<xsl:value-of select="{Value}" />
</setSpec>
</xsl:template>
@ -406,7 +406,7 @@
<xsl:text>doc-type:ResearchData</xsl:text>
</dc:type>
<!-- dc:format -->
<xsl:apply-templates select="File/@MimeType" mode="oai_dc" />
<xsl:apply-templates select="{File/MimeType}" mode="oai_dc" />
<!-- <dc:format> -->
<xsl:apply-templates select="File" mode="oai_dc" />
<!-- dc:identifier -->

View File

@ -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;

View File

@ -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<number, string>([
[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];
// }
// }