Throw message for ValidationExceptions

This commit is contained in:
Daniel Cortés
2021-04-28 20:07:11 +02:00
committed by Jonathan Putney
parent 7b92c65061
commit 67bf600c52
9 changed files with 156 additions and 70 deletions

View File

@@ -9,6 +9,7 @@ import {
} from './scorm12_cmi';
const aicc_constants = APIConstants.aicc;
const scorm12_constants = APIConstants.scorm12;
const aicc_regex = Regex.aicc;
const scorm12_error_codes = ErrorCodes.scorm12;
@@ -126,8 +127,11 @@ class CMIEvaluationComments extends CMIArray {
* Constructor for AICC Evaluation Comments object
*/
constructor() {
super(aicc_constants.comments_children,
scorm12_error_codes.INVALID_SET_VALUE);
super({
children: aicc_constants.comments_children,
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
});
}
}
@@ -143,6 +147,7 @@ class AICCStudentPreferences extends Scorm12CMI.CMIStudentPreference {
this.windows = new CMIArray({
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
children: '',
});
}
@@ -671,7 +676,7 @@ export class CMIPaths extends CMIArray {
* Constructor for inline Paths Array class
*/
constructor() {
super(aicc_constants.paths_children);
super({children: aicc_constants.paths_children});
}
}
@@ -837,7 +842,7 @@ export class CMITries extends CMIArray {
* Constructor for inline Tries Array class
*/
constructor() {
super(aicc_constants.tries_children);
super({children: aicc_constants.tries_children});
}
}
@@ -856,8 +861,11 @@ export class CMITriesObject extends BaseCMI {
score_children: aicc_constants.score_children,
score_range: aicc_regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidErrorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidTypeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.TYPE_MISMATCH].detailMessage,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
invalidRangeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.VALUE_OUT_OF_RANGE].detailMessage,
});
}
@@ -938,7 +946,7 @@ export class CMIAttemptRecords extends CMIArray {
* Constructor for inline Tries Array class
*/
constructor() {
super(aicc_constants.attempt_records_children);
super({children: aicc_constants.attempt_records_children});
}
}
@@ -957,8 +965,11 @@ export class CMIAttemptRecordsObject extends BaseCMI {
score_children: aicc_constants.score_children,
score_range: aicc_regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidErrorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidTypeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.TYPE_MISMATCH].detailMessage,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
invalidRangeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.VALUE_OUT_OF_RANGE].detailMessage,
});
}

View File

@@ -14,6 +14,7 @@ const scorm12_error_codes = ErrorCodes.scorm12;
* @param {string} value
* @param {string} regexPattern
* @param {number} errorCode
* @param {string} errorMessage
* @param {boolean} allowEmptyString
* @return {boolean}
*/
@@ -21,6 +22,7 @@ export function checkValidFormat(
value: String,
regexPattern: String,
errorCode: number,
errorMessage: String,
allowEmptyString?: boolean) {
const formatRegex = new RegExp(regexPattern);
const matches = value.match(formatRegex);
@@ -28,7 +30,7 @@ export function checkValidFormat(
return true;
}
if (value === undefined || !matches || matches[0] === '') {
throw new ValidationError(errorCode);
throw new ValidationError(errorCode, errorMessage);
}
return true;
}
@@ -39,20 +41,21 @@ export function checkValidFormat(
* @param {*} value
* @param {string} rangePattern
* @param {number} errorCode
* @param {string} errorMessage
* @return {boolean}
*/
export function checkValidRange(
value: any, rangePattern: String, errorCode: number) {
value: any, rangePattern: String, errorCode: number, errorMessage: String) {
const ranges = rangePattern.split('#');
value = value * 1.0;
if (value >= ranges[0]) {
if ((ranges[1] === '*') || (value <= ranges[1])) {
return true;
} else {
throw new ValidationError(errorCode);
throw new ValidationError(errorCode, errorMessage);
}
} else {
throw new ValidationError(errorCode);
throw new ValidationError(errorCode, errorMessage);
}
}
@@ -115,8 +118,11 @@ export class CMIScore extends BaseCMI {
* @param {string} score_range
* @param {string} max
* @param {number} invalidErrorCode
* @param {string} invalidErrorMessage
* @param {number} invalidTypeCode
* @param {string} invalidTypeMessage
* @param {number} invalidRangeCode
* @param {string} invalidRangeMessage
* @param {string} decimalRegex
*/
constructor(
@@ -125,8 +131,11 @@ export class CMIScore extends BaseCMI {
score_range,
max,
invalidErrorCode,
invalidErrorMessage,
invalidTypeCode,
invalidTypeMessage,
invalidRangeCode,
invalidRangeMessage,
decimalRegex,
}) {
super();
@@ -137,10 +146,16 @@ export class CMIScore extends BaseCMI {
this.#max = (max || max === '') ? max : '100';
this.#_invalid_error_code = invalidErrorCode ||
scorm12_error_codes.INVALID_SET_VALUE;
this.#_invalid_error_message = invalidErrorMessage ||
scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage;
this.#_invalid_type_code = invalidTypeCode ||
scorm12_error_codes.TYPE_MISMATCH;
this.#_invalid_type_message = invalidTypeMessage ||
scorm12_constants.error_descriptions[scorm12_error_codes.TYPE_MISMATCH].detailMessage;
this.#_invalid_range_code = invalidRangeCode ||
scorm12_error_codes.VALUE_OUT_OF_RANGE;
this.#_invalid_range_message = invalidRangeMessage ||
scorm12_constants.error_descriptions[scorm12_error_codes.VALUE_OUT_OF_RANGE].detailMessage;
this.#_decimal_regex = decimalRegex ||
scorm12_regex.CMIDecimal;
}
@@ -148,8 +163,11 @@ export class CMIScore extends BaseCMI {
#_children;
#_score_range;
#_invalid_error_code;
#_invalid_error_message;
#_invalid_type_code;
#_invalid_type_message;
#_invalid_range_code;
#_invalid_range_message;
#_decimal_regex;
#raw = '';
#min = '';
@@ -170,7 +188,7 @@ export class CMIScore extends BaseCMI {
* @private
*/
set _children(_children) {
throw new ValidationError(this.#_invalid_error_code);
throw new ValidationError(this.#_invalid_error_code, this.#_invalid_error_message);
}
/**
@@ -187,10 +205,10 @@ export class CMIScore extends BaseCMI {
*/
set raw(raw) {
if (checkValidFormat(raw, this.#_decimal_regex,
this.#_invalid_type_code) &&
this.#_invalid_type_code, this.#_invalid_type_message) &&
(!this.#_score_range ||
checkValidRange(raw, this.#_score_range,
this.#_invalid_range_code))) {
this.#_invalid_range_code, this.#_invalid_range_message))) {
this.#raw = raw;
}
}
@@ -209,10 +227,10 @@ export class CMIScore extends BaseCMI {
*/
set min(min) {
if (checkValidFormat(min, this.#_decimal_regex,
this.#_invalid_type_code) &&
this.#_invalid_type_code, this.#_invalid_type_message) &&
(!this.#_score_range ||
checkValidRange(min, this.#_score_range,
this.#_invalid_range_code))) {
this.#_invalid_range_code, this.#_invalid_range_message))) {
this.#min = min;
}
}
@@ -231,10 +249,10 @@ export class CMIScore extends BaseCMI {
*/
set max(max) {
if (checkValidFormat(max, this.#_decimal_regex,
this.#_invalid_type_code) &&
this.#_invalid_type_code, this.#_invalid_type_message) &&
(!this.#_score_range ||
checkValidRange(max, this.#_score_range,
this.#_invalid_range_code))) {
this.#_invalid_range_code, this.#_invalid_range_message))) {
this.#max = max;
}
}
@@ -263,15 +281,18 @@ export class CMIArray extends BaseCMI {
* Constructor cmi *.n arrays
* @param {string} children
* @param {number} errorCode
* @param {string} errorMessage
*/
constructor({children, errorCode}) {
constructor({children, errorCode, errorMessage}) {
super();
this.#_children = children;
this.#errorCode = errorCode;
this.#errorMessage = errorMessage;
this.childArray = [];
}
#errorCode;
#errorMessage;
#_children;
/**
@@ -287,7 +308,7 @@ export class CMIArray extends BaseCMI {
* @param {string} _children
*/
set _children(_children) {
throw new ValidationError(this.#errorCode);
throw new ValidationError(this.#errorCode, this.#errorMessage);
}
/**
@@ -303,7 +324,7 @@ export class CMIArray extends BaseCMI {
* @param {number} _count
*/
set _count(_count) {
throw new ValidationError(this.#errorCode);
throw new ValidationError(this.#errorCode, this.#errorMessage);
}
/**

View File

@@ -21,21 +21,30 @@ const scorm12_error_codes = ErrorCodes.scorm12;
* Helper method for throwing Read Only error
*/
export function throwReadOnlyError() {
throw new ValidationError(scorm12_error_codes.READ_ONLY_ELEMENT);
throw new ValidationError(
scorm12_error_codes.READ_ONLY_ELEMENT,
scorm12_constants.error_descriptions[scorm12_error_codes.READ_ONLY_ELEMENT].detailMessage
);
}
/**
* Helper method for throwing Write Only error
*/
export function throwWriteOnlyError() {
throw new ValidationError(scorm12_error_codes.WRITE_ONLY_ELEMENT);
throw new ValidationError(
scorm12_error_codes.WRITE_ONLY_ELEMENT,
scorm12_constants.error_descriptions[scorm12_error_codes.WRITE_ONLY_ELEMENT].detailMessage
);
}
/**
* Helper method for throwing Invalid Set error
*/
function throwInvalidValueError() {
throw new ValidationError(scorm12_error_codes.INVALID_SET_VALUE);
throw new ValidationError(
scorm12_error_codes.INVALID_SET_VALUE,
scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage
);
}
/**
@@ -50,7 +59,9 @@ export function check12ValidFormat(
regexPattern: String,
allowEmptyString?: boolean) {
return checkValidFormat(value, regexPattern,
scorm12_error_codes.TYPE_MISMATCH, allowEmptyString);
scorm12_error_codes.TYPE_MISMATCH,
scorm12_constants.error_descriptions[scorm12_error_codes.TYPE_MISMATCH].detailMessage,
allowEmptyString);
}
/**
@@ -65,7 +76,9 @@ export function check12ValidRange(
rangePattern: String,
allowEmptyString?: boolean) {
return checkValidRange(value, rangePattern,
scorm12_error_codes.VALUE_OUT_OF_RANGE, allowEmptyString);
scorm12_error_codes.VALUE_OUT_OF_RANGE,
scorm12_constants.error_descriptions[scorm12_error_codes.VALUE_OUT_OF_RANGE].detailMessage,
allowEmptyString);
}
/**
@@ -275,8 +288,11 @@ class CMICore extends BaseCMI {
score_children: scorm12_constants.score_children,
score_range: scorm12_regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidErrorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidTypeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.TYPE_MISMATCH].detailMessage,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
invalidRangeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.VALUE_OUT_OF_RANGE].detailMessage,
});
}
@@ -583,6 +599,7 @@ class CMIObjectives extends CMIArray {
super({
children: scorm12_constants.objectives_children,
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
});
}
}
@@ -858,6 +875,7 @@ class CMIInteractions extends CMIArray {
super({
children: scorm12_constants.interactions_children,
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
});
}
}
@@ -875,10 +893,12 @@ export class CMIInteractionsObject extends BaseCMI {
this.objectives = new CMIArray({
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
children: scorm12_constants.objectives_children,
});
this.correct_responses = new CMIArray({
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
children: scorm12_constants.correct_responses_children,
});
}
@@ -1080,8 +1100,11 @@ export class CMIObjectivesObject extends BaseCMI {
score_children: scorm12_constants.score_children,
score_range: scorm12_regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidErrorMessage: scorm12_constants.error_descriptions[scorm12_error_codes.INVALID_SET_VALUE].detailMessage,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidTypeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.TYPE_MISMATCH].detailMessage,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
invalidRangeMessage: scorm12_constants.error_descriptions[scorm12_error_codes.VALUE_OUT_OF_RANGE].detailMessage,
});
}

View File

@@ -23,21 +23,50 @@ const scorm2004_regex = Regex.scorm2004;
* Helper method for throwing Read Only error
*/
function throwReadOnlyError() {
throw new ValidationError(scorm2004_error_codes.READ_ONLY_ELEMENT);
throw new ValidationError(
scorm2004_error_codes.READ_ONLY_ELEMENT,
scorm2004_constants.error_descriptions[scorm2004_error_codes.READ_ONLY_ELEMENT].detailMessage
);
}
/**
* Helper method for throwing Write Only error
*/
function throwWriteOnlyError() {
throw new ValidationError(scorm2004_error_codes.WRITE_ONLY_ELEMENT);
throw new ValidationError(
scorm2004_error_codes.WRITE_ONLY_ELEMENT,
scorm2004_constants.error_descriptions[scorm2004_error_codes.WRITE_ONLY_ELEMENT].detailMessage
);
}
/**
* Helper method for throwing Type Mismatch error
*/
function throwTypeMismatchError() {
throw new ValidationError(scorm2004_error_codes.TYPE_MISMATCH);
throw new ValidationError(
scorm2004_error_codes.TYPE_MISMATCH,
scorm2004_constants.error_descriptions[scorm2004_error_codes.TYPE_MISMATCH].detailMessage
);
}
/**
* Helper method for throwing Dependency Not Established error
*/
function throwDependencyNotEstablishedError() {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED,
scorm2004_constants.error_descriptions[scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED].detailMessage
);
}
/**
* Helper method for throwing Dependency Not Established error
*/
function throwGeneralSetError() {
throw new ValidationError(
scorm2004_error_codes.GENERAL_SET_FAILURE,
scorm2004_constants.error_descriptions[scorm2004_error_codes.GENERAL_SET_FAILURE].detailMessage
);
}
/**
@@ -52,7 +81,9 @@ function check2004ValidFormat(
regexPattern: String,
allowEmptyString?: boolean) {
return checkValidFormat(value, regexPattern,
scorm2004_error_codes.TYPE_MISMATCH, allowEmptyString);
scorm2004_error_codes.TYPE_MISMATCH,
scorm2004_constants.error_descriptions[scorm2004_error_codes.TYPE_MISMATCH].detailMessage,
allowEmptyString);
}
/**
@@ -63,7 +94,8 @@ function check2004ValidFormat(
*/
function check2004ValidRange(value: any, rangePattern: String) {
return checkValidRange(value, rangePattern,
scorm2004_error_codes.VALUE_OUT_OF_RANGE);
scorm2004_error_codes.VALUE_OUT_OF_RANGE,
scorm2004_constants.error_descriptions[scorm2004_error_codes.VALUE_OUT_OF_RANGE].detailMessage);
}
/**
@@ -701,6 +733,7 @@ class CMIInteractions extends CMIArray {
super({
children: scorm2004_constants.interactions_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.READ_ONLY_ELEMENT].detailMessage,
});
}
}
@@ -716,6 +749,7 @@ class CMIObjectives extends CMIArray {
super({
children: scorm2004_constants.objectives_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.READ_ONLY_ELEMENT].detailMessage,
});
}
}
@@ -731,6 +765,7 @@ class CMICommentsFromLMS extends CMIArray {
super({
children: scorm2004_constants.comments_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.READ_ONLY_ELEMENT].detailMessage,
});
}
}
@@ -746,6 +781,7 @@ class CMICommentsFromLearner extends CMIArray {
super({
children: scorm2004_constants.comments_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.READ_ONLY_ELEMENT].detailMessage,
});
}
}
@@ -771,10 +807,12 @@ export class CMIInteractionsObject extends BaseCMI {
this.objectives = new CMIArray({
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.INVALID_SET_VALUE].detailMessage,
children: scorm2004_constants.objectives_children,
});
this.correct_responses = new CMIArray({
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.INVALID_SET_VALUE].detailMessage,
children: scorm2004_constants.correct_responses_children,
});
}
@@ -820,8 +858,7 @@ export class CMIInteractionsObject extends BaseCMI {
*/
set type(type) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(type, scorm2004_regex.CMIType)) {
this.#type = type;
@@ -843,8 +880,7 @@ export class CMIInteractionsObject extends BaseCMI {
*/
set timestamp(timestamp) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(timestamp, scorm2004_regex.CMITime)) {
this.#timestamp = timestamp;
@@ -866,8 +902,7 @@ export class CMIInteractionsObject extends BaseCMI {
*/
set weighting(weighting) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(weighting, scorm2004_regex.CMIDecimal)) {
this.#weighting = weighting;
@@ -890,8 +925,7 @@ export class CMIInteractionsObject extends BaseCMI {
*/
set learner_response(learner_response) {
if (this.initialized && (this.#type === '' || this.#id === '')) {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
let nodes = [];
const response_type = learner_responses[this.type];
@@ -933,12 +967,12 @@ export class CMIInteractionsObject extends BaseCMI {
}
}
} else {
throw new ValidationError(scorm2004_error_codes.GENERAL_SET_FAILURE);
throwGeneralSetError();
}
this.#learner_response = learner_response;
} else {
throw new ValidationError(scorm2004_error_codes.TYPE_MISMATCH);
throwTypeMismatchError();
}
}
}
@@ -975,8 +1009,7 @@ export class CMIInteractionsObject extends BaseCMI {
*/
set latency(latency) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(latency, scorm2004_regex.CMITimespan)) {
this.#latency = latency;
@@ -998,8 +1031,7 @@ export class CMIInteractionsObject extends BaseCMI {
*/
set description(description) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(description, scorm2004_regex.CMILangString250,
true)) {
@@ -1104,8 +1136,7 @@ export class CMIObjectivesObject extends BaseCMI {
*/
set success_status(success_status) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(success_status, scorm2004_regex.CMISStatus)) {
this.#success_status = success_status;
@@ -1127,8 +1158,7 @@ export class CMIObjectivesObject extends BaseCMI {
*/
set completion_status(completion_status) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(completion_status, scorm2004_regex.CMICStatus)) {
this.#completion_status = completion_status;
@@ -1150,8 +1180,7 @@ export class CMIObjectivesObject extends BaseCMI {
*/
set progress_measure(progress_measure) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(progress_measure, scorm2004_regex.CMIDecimal) &&
check2004ValidRange(progress_measure,
@@ -1175,8 +1204,7 @@ export class CMIObjectivesObject extends BaseCMI {
*/
set description(description) {
if (this.initialized && this.#id === '') {
throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
throwDependencyNotEstablishedError();
} else {
if (check2004ValidFormat(description, scorm2004_regex.CMILangString250,
true)) {
@@ -1229,8 +1257,11 @@ class Scorm2004CMIScore extends CMIScore {
score_children: scorm2004_constants.score_children,
max: '',
invalidErrorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
invalidErrorMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.READ_ONLY_ELEMENT].detailMessage,
invalidTypeCode: scorm2004_error_codes.TYPE_MISMATCH,
invalidTypeMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.TYPE_MISMATCH].detailMessage,
invalidRangeCode: scorm2004_error_codes.VALUE_OUT_OF_RANGE,
invalidRangeMessage: scorm2004_constants.error_descriptions[scorm2004_error_codes.VALUE_OUT_OF_RANGE].detailMessage,
decimalRegex: scorm2004_regex.CMIDecimal,
});
}

View File

@@ -66,6 +66,14 @@ const scorm12 = {
basicMessage: 'Incorrect Data Type',
detailMessage: 'LMSSetValue was called with a value that is not consistent with the data format of the supplied data model element.',
},
'407': {
basicMessage: 'Element Value Out Of Range',
detailMessage: 'The numeric value supplied to a LMSSetValue call is outside of the numeric range allowed for the supplied data model element.',
},
'408': {
basicMessage: 'Data Model Dependency Not Established',
detailMessage: 'Some data model elements cannot be set until another data model element was set. This error condition indicates that the prerequisite element was not set before the dependent element.',
},
},
};

View File

@@ -8,8 +8,8 @@ export class ValidationError extends Error {
* Constructor to take in an error message and code
* @param {number} errorCode
*/
constructor(errorCode: number) {
super(errorCode);
constructor(errorCode: number, ...rest) {
super(...rest);
this.#errorCode = errorCode;
}
@@ -22,12 +22,4 @@ export class ValidationError extends Error {
get errorCode() {
return this.#errorCode;
}
/**
* Trying to override the default Error message
* @return {string}
*/
get message() {
return this.#errorCode + '';
}
}

View File

@@ -45,7 +45,7 @@ export const checkLMSSetValue = (
if (expectedError > 0) {
if (errorThrown) {
expect(() => api.lmsSetValue(fieldName, valueToTest)).
to.throw(String(expectedError));
to.throw().with.property('errorCode', expectedError);
} else {
api.lmsSetValue(fieldName, valueToTest);
expect(String(api.lmsGetLastError())).to.equal(String(expectedError));
@@ -84,7 +84,7 @@ export const checkLMSGetValue = (
if (expectedError > 0) {
if (errorThrown) {
expect(() => api.lmsGetValue(fieldName)).
to.throw(String(expectedError));
to.throw().with.property('errorCode', expectedError);
} else {
api.lmsGetValue(fieldName);
expect(String(api.lmsGetLastError())).to.equal(String(expectedError));
@@ -110,7 +110,7 @@ export const checkSetCMIValue = (
if (expectedError > 0) {
if (errorThrown) {
expect(() => api.setCMIValue(fieldName, valueToTest)).
to.throw(String(expectedError));
to.throw().with.property('errorCode', expectedError);
} else {
api.setCMIValue(fieldName, valueToTest);
expect(String(api.lmsGetLastError())).to.equal(String(expectedError));

View File

@@ -23,7 +23,7 @@ export const checkFieldConstraintSize = (
it(`Should fail to write more than ${limit} characters to ${fieldName}`,
() => {
expect(() => eval(`${fieldName} = 'x'.repeat(${limit + 1})`)).
to.throw(expectedError + '');
to.throw().with.property('errorCode', expectedError);
});
});
};
@@ -42,7 +42,7 @@ export const checkReadOnly = (
it(`Should fail to write to ${fieldName}`, () => {
expect(() => eval(`${fieldName} = 'xxx'`)).
to.throw(expectedError + '');
to.throw().with.property('errorCode', expectedError);
});
});
};
@@ -89,7 +89,7 @@ export const checkWriteOnly = (
describe(`Field: ${fieldName}`, () => {
it(`Should fail to read from ${fieldName}`, () => {
expect(() => eval(`${fieldName}`)).
to.throw(expectedError + '');
to.throw().with.property('errorCode', expectedError);
});
it(`Should successfully write to ${fieldName}`, () => {

View File

@@ -5,12 +5,12 @@ import {ValidationError} from '../src/exceptions';
describe('Exception Tests', () => {
it('ValidationException should return message string', () => {
expect(
new ValidationError(0).message,
).to.equal('0');
new ValidationError(0, 'Error Message').message,
).to.equal('Error Message');
});
it('ValidationException should return errorCode number', () => {
expect(
new ValidationError(0).errorCode,
new ValidationError(0, 'Error Message').errorCode,
).to.equal(0);
});
});