Merge branch 'master' into issue/283

This commit is contained in:
Jonathan Putney
2021-05-27 11:02:44 -04:00
committed by GitHub
18 changed files with 1608 additions and 882 deletions

View File

@@ -28,6 +28,7 @@ export default class BaseAPI {
logLevel: global_constants.LOG_LEVEL_ERROR,
selfReportSessionTime: false,
alwaysSendTotalTime: false,
strict_errors: true,
responseHandler: function(xhr) {
let result;
if (typeof xhr !== 'undefined') {
@@ -185,7 +186,21 @@ export default class BaseAPI {
this.#error_codes.RETRIEVE_BEFORE_INIT,
this.#error_codes.RETRIEVE_AFTER_TERM)) {
if (checkTerminated) this.lastErrorCode = 0;
returnValue = this.getCMIValue(CMIElement);
try {
returnValue = this.getCMIValue(CMIElement);
} catch (e) {
if (e instanceof ValidationError) {
this.lastErrorCode = e.errorCode;
returnValue = global_constants.SCORM_FALSE;
} else {
if (e.message) {
console.error(e.message);
} else {
console.error(e);
}
this.throwSCORMError(this.#error_codes.GENERAL);
}
}
this.processListeners(callbackName, CMIElement);
}

View File

@@ -202,7 +202,7 @@ export default class Scorm12API extends BaseAPI {
* Returns the message that corresponds to errorNumber.
*
* @param {*} errorNumber
* @param {boolean }detail
* @param {boolean} detail
* @return {string}
*/
getLmsErrorMessageDetails(errorNumber, detail) {
@@ -278,8 +278,7 @@ export default class Scorm12API extends BaseAPI {
if (this.settings.mastery_override &&
this.cmi.student_data.mastery_score !== '' &&
this.cmi.core.score.raw !== '') {
if (parseFloat(this.cmi.core.score.raw) >=
parseFloat(this.cmi.student_data.mastery_score)) {
if (parseFloat(this.cmi.core.score.raw) >= parseFloat(this.cmi.student_data.mastery_score)) {
this.cmi.core.lesson_status = 'passed';
} else {
this.cmi.core.lesson_status = 'failed';
@@ -287,8 +286,7 @@ export default class Scorm12API extends BaseAPI {
}
}
} else if (this.cmi.core.lesson_mode === 'browse') {
if ((this.startingData?.cmi?.core?.lesson_status || '') === '' &&
originalStatus === 'not attempted') {
if ((this.startingData?.cmi?.core?.lesson_status || '') === '' && originalStatus === 'not attempted') {
this.cmi.core.lesson_status = 'browsed';
}
}
@@ -298,13 +296,11 @@ export default class Scorm12API extends BaseAPI {
this.settings.alwaysSendTotalTime);
if (this.apiLogLevel === global_constants.LOG_LEVEL_DEBUG) {
console.debug('Commit (terminated: ' +
(terminateCommit ? 'yes' : 'no') + '): ');
console.debug('Commit (terminated: ' + (terminateCommit ? 'yes' : 'no') + '): ');
console.debug(commitObject);
}
if (this.settings.lmsCommitUrl) {
return this.processHttpRequest(this.settings.lmsCommitUrl, commitObject,
terminateCommit);
return this.processHttpRequest(this.settings.lmsCommitUrl, commitObject, terminateCommit);
} else {
return global_constants.SCORM_TRUE;
}

View File

@@ -1,16 +1,40 @@
import * as Scorm12CMI from './scorm12_cmi';
import {BaseCMI, CMIArray, CMIScore} from './common';
import {BaseCMI, checkValidFormat, CMIArray, CMIScore} from './common';
import APIConstants from '../constants/api_constants';
import Regex from '../constants/regex';
import ErrorCodes from '../constants/error_codes';
import {
check12ValidFormat,
throwReadOnlyError,
} from './scorm12_cmi';
import {AICCValidationError} from '../exceptions';
const aicc_constants = APIConstants.aicc;
const aicc_regex = Regex.aicc;
const scorm12_error_codes = ErrorCodes.scorm12;
const aicc_error_codes = ErrorCodes.scorm12;
/**
* Helper method for throwing Read Only error
*/
function throwReadOnlyError() {
throw new AICCValidationError(aicc_error_codes.READ_ONLY_ELEMENT);
}
/**
* Helper method, no reason to have to pass the same error codes every time
* @param {*} value
* @param {string} regexPattern
* @param {boolean} allowEmptyString
* @return {boolean}
*/
function checkAICCValidFormat(
value: String,
regexPattern: String,
allowEmptyString?: boolean) {
return checkValidFormat(
value,
regexPattern,
aicc_error_codes.TYPE_MISMATCH,
AICCValidationError,
allowEmptyString
);
}
/**
* CMI Class for AICC
@@ -126,8 +150,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: aicc_error_codes.INVALID_SET_VALUE,
errorClass: AICCValidationError,
});
}
}
@@ -142,7 +169,8 @@ class AICCStudentPreferences extends Scorm12CMI.CMIStudentPreference {
super(aicc_constants.student_preference_children);
this.windows = new CMIArray({
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorCode: aicc_error_codes.INVALID_SET_VALUE,
errorClass: AICCValidationError,
children: '',
});
}
@@ -174,7 +202,7 @@ class AICCStudentPreferences extends Scorm12CMI.CMIStudentPreference {
* @param {string} lesson_type
*/
set lesson_type(lesson_type: string) {
if (check12ValidFormat(lesson_type, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(lesson_type, aicc_regex.CMIString256)) {
this.#lesson_type = lesson_type;
}
}
@@ -192,7 +220,7 @@ class AICCStudentPreferences extends Scorm12CMI.CMIStudentPreference {
* @param {string} text_color
*/
set text_color(text_color: string) {
if (check12ValidFormat(text_color, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(text_color, aicc_regex.CMIString256)) {
this.#text_color = text_color;
}
}
@@ -210,7 +238,7 @@ class AICCStudentPreferences extends Scorm12CMI.CMIStudentPreference {
* @param {string} text_location
*/
set text_location(text_location: string) {
if (check12ValidFormat(text_location, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(text_location, aicc_regex.CMIString256)) {
this.#text_location = text_location;
}
}
@@ -228,7 +256,7 @@ class AICCStudentPreferences extends Scorm12CMI.CMIStudentPreference {
* @param {string} text_size
*/
set text_size(text_size: string) {
if (check12ValidFormat(text_size, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(text_size, aicc_regex.CMIString256)) {
this.#text_size = text_size;
}
}
@@ -246,7 +274,7 @@ class AICCStudentPreferences extends Scorm12CMI.CMIStudentPreference {
* @param {string} video
*/
set video(video: string) {
if (check12ValidFormat(video, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(video, aicc_regex.CMIString256)) {
this.#video = video;
}
}
@@ -374,6 +402,14 @@ export class CMIStudentDemographics extends BaseCMI {
#telephone = '';
#years_experience = '';
/**
* Getter for _children
* @return {string}
*/
get _children() {
return this.#_children;
}
/**
* Getter for city
* @return {string}
@@ -671,7 +707,7 @@ export class CMIPaths extends CMIArray {
* Constructor for inline Paths Array class
*/
constructor() {
super(aicc_constants.paths_children);
super({children: aicc_constants.paths_children});
}
}
@@ -706,7 +742,7 @@ export class CMIPathsObject extends BaseCMI {
* @param {string} location_id
*/
set location_id(location_id) {
if (check12ValidFormat(location_id, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(location_id, aicc_regex.CMIString256)) {
this.#location_id = location_id;
}
}
@@ -724,7 +760,7 @@ export class CMIPathsObject extends BaseCMI {
* @param {string} date
*/
set date(date) {
if (check12ValidFormat(date, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(date, aicc_regex.CMIString256)) {
this.#date = date;
}
}
@@ -742,7 +778,7 @@ export class CMIPathsObject extends BaseCMI {
* @param {string} time
*/
set time(time) {
if (check12ValidFormat(time, aicc_regex.CMITime)) {
if (checkAICCValidFormat(time, aicc_regex.CMITime)) {
this.#time = time;
}
}
@@ -760,7 +796,7 @@ export class CMIPathsObject extends BaseCMI {
* @param {string} status
*/
set status(status) {
if (check12ValidFormat(status, aicc_regex.CMIStatus2)) {
if (checkAICCValidFormat(status, aicc_regex.CMIStatus2)) {
this.#status = status;
}
}
@@ -778,7 +814,7 @@ export class CMIPathsObject extends BaseCMI {
* @param {string} why_left
*/
set why_left(why_left) {
if (check12ValidFormat(why_left, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(why_left, aicc_regex.CMIString256)) {
this.#why_left = why_left;
}
}
@@ -796,7 +832,7 @@ export class CMIPathsObject extends BaseCMI {
* @param {string} time_in_element
*/
set time_in_element(time_in_element) {
if (check12ValidFormat(time_in_element, aicc_regex.CMITime)) {
if (checkAICCValidFormat(time_in_element, aicc_regex.CMITime)) {
this.#time_in_element = time_in_element;
}
}
@@ -837,7 +873,7 @@ export class CMITries extends CMIArray {
* Constructor for inline Tries Array class
*/
constructor() {
super(aicc_constants.tries_children);
super({children: aicc_constants.tries_children});
}
}
@@ -855,9 +891,10 @@ export class CMITriesObject extends BaseCMI {
{
score_children: aicc_constants.score_children,
score_range: aicc_regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
invalidErrorCode: aicc_error_codes.INVALID_SET_VALUE,
invalidTypeCode: aicc_error_codes.TYPE_MISMATCH,
invalidRangeCode: aicc_error_codes.VALUE_OUT_OF_RANGE,
errorClass: AICCValidationError,
});
}
@@ -885,7 +922,7 @@ export class CMITriesObject extends BaseCMI {
* @param {string} status
*/
set status(status) {
if (check12ValidFormat(status, aicc_regex.CMIStatus2)) {
if (checkAICCValidFormat(status, aicc_regex.CMIStatus2)) {
this.#status = status;
}
}
@@ -903,7 +940,7 @@ export class CMITriesObject extends BaseCMI {
* @param {string} time
*/
set time(time) {
if (check12ValidFormat(time, aicc_regex.CMITime)) {
if (checkAICCValidFormat(time, aicc_regex.CMITime)) {
this.#time = time;
}
}
@@ -938,7 +975,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});
}
}
@@ -956,9 +993,10 @@ export class CMIAttemptRecordsObject extends BaseCMI {
{
score_children: aicc_constants.score_children,
score_range: aicc_regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
invalidErrorCode: aicc_error_codes.INVALID_SET_VALUE,
invalidTypeCode: aicc_error_codes.TYPE_MISMATCH,
invalidRangeCode: aicc_error_codes.VALUE_OUT_OF_RANGE,
errorClass: AICCValidationError,
});
}
@@ -985,7 +1023,7 @@ export class CMIAttemptRecordsObject extends BaseCMI {
* @param {string} lesson_status
*/
set lesson_status(lesson_status) {
if (check12ValidFormat(lesson_status, aicc_regex.CMIStatus2)) {
if (checkAICCValidFormat(lesson_status, aicc_regex.CMIStatus2)) {
this.#lesson_status = lesson_status;
}
}
@@ -1039,7 +1077,7 @@ export class CMIEvaluationCommentsObject extends BaseCMI {
* @param {string} content
*/
set content(content) {
if (check12ValidFormat(content, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(content, aicc_regex.CMIString256)) {
this.#content = content;
}
}
@@ -1057,7 +1095,7 @@ export class CMIEvaluationCommentsObject extends BaseCMI {
* @param {string} location
*/
set location(location) {
if (check12ValidFormat(location, aicc_regex.CMIString256)) {
if (checkAICCValidFormat(location, aicc_regex.CMIString256)) {
this.#location = location;
}
}
@@ -1075,7 +1113,7 @@ export class CMIEvaluationCommentsObject extends BaseCMI {
* @param {string} time
*/
set time(time) {
if (check12ValidFormat(time, aicc_regex.CMITime)) {
if (checkAICCValidFormat(time, aicc_regex.CMITime)) {
this.#time = time;
}
}

View File

@@ -1,7 +1,6 @@
// @flow
import APIConstants from '../constants/api_constants';
import ErrorCodes from '../constants/error_codes';
import {ValidationError} from '../exceptions';
import Regex from '../constants/regex';
const scorm12_constants = APIConstants.scorm12;
@@ -14,6 +13,7 @@ const scorm12_error_codes = ErrorCodes.scorm12;
* @param {string} value
* @param {string} regexPattern
* @param {number} errorCode
* @param {class} errorClass
* @param {boolean} allowEmptyString
* @return {boolean}
*/
@@ -21,6 +21,7 @@ export function checkValidFormat(
value: String,
regexPattern: String,
errorCode: number,
errorClass: function,
allowEmptyString?: boolean) {
const formatRegex = new RegExp(regexPattern);
const matches = value.match(formatRegex);
@@ -28,7 +29,7 @@ export function checkValidFormat(
return true;
}
if (value === undefined || !matches || matches[0] === '') {
throw new ValidationError(errorCode);
throw new errorClass.prototype.constructor(errorCode);
}
return true;
}
@@ -39,20 +40,24 @@ export function checkValidFormat(
* @param {*} value
* @param {string} rangePattern
* @param {number} errorCode
* @param {class} errorClass
* @return {boolean}
*/
export function checkValidRange(
value: any, rangePattern: String, errorCode: number) {
value: any,
rangePattern: String,
errorCode: number,
errorClass: function) {
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 errorClass.prototype.constructor(errorCode);
}
} else {
throw new ValidationError(errorCode);
throw new errorClass.prototype.constructor(errorCode);
}
}
@@ -118,6 +123,7 @@ export class CMIScore extends BaseCMI {
* @param {number} invalidTypeCode
* @param {number} invalidRangeCode
* @param {string} decimalRegex
* @param {class} errorClass
*/
constructor(
{
@@ -128,6 +134,7 @@ export class CMIScore extends BaseCMI {
invalidTypeCode,
invalidRangeCode,
decimalRegex,
errorClass,
}) {
super();
@@ -143,6 +150,7 @@ export class CMIScore extends BaseCMI {
scorm12_error_codes.VALUE_OUT_OF_RANGE;
this.#_decimal_regex = decimalRegex ||
scorm12_regex.CMIDecimal;
this.#_error_class = errorClass;
}
#_children;
@@ -151,6 +159,7 @@ export class CMIScore extends BaseCMI {
#_invalid_type_code;
#_invalid_range_code;
#_decimal_regex;
#_error_class;
#raw = '';
#min = '';
#max;
@@ -170,7 +179,7 @@ export class CMIScore extends BaseCMI {
* @private
*/
set _children(_children) {
throw new ValidationError(this.#_invalid_error_code);
throw new this.#_error_class.prototype.constructor(this.#_invalid_error_code);
}
/**
@@ -186,11 +195,9 @@ export class CMIScore extends BaseCMI {
* @param {string} raw
*/
set raw(raw) {
if (checkValidFormat(raw, this.#_decimal_regex,
this.#_invalid_type_code) &&
if (checkValidFormat(raw, this.#_decimal_regex, this.#_invalid_type_code, this.#_error_class) &&
(!this.#_score_range ||
checkValidRange(raw, this.#_score_range,
this.#_invalid_range_code))) {
checkValidRange(raw, this.#_score_range, this.#_invalid_range_code, this.#_error_class))) {
this.#raw = raw;
}
}
@@ -208,11 +215,9 @@ export class CMIScore extends BaseCMI {
* @param {string} min
*/
set min(min) {
if (checkValidFormat(min, this.#_decimal_regex,
this.#_invalid_type_code) &&
if (checkValidFormat(min, this.#_decimal_regex, this.#_invalid_type_code, this.#_error_class) &&
(!this.#_score_range ||
checkValidRange(min, this.#_score_range,
this.#_invalid_range_code))) {
checkValidRange(min, this.#_score_range, this.#_invalid_range_code, this.#_error_class))) {
this.#min = min;
}
}
@@ -230,11 +235,9 @@ export class CMIScore extends BaseCMI {
* @param {string} max
*/
set max(max) {
if (checkValidFormat(max, this.#_decimal_regex,
this.#_invalid_type_code) &&
if (checkValidFormat(max, this.#_decimal_regex, this.#_invalid_type_code, this.#_error_class) &&
(!this.#_score_range ||
checkValidRange(max, this.#_score_range,
this.#_invalid_range_code))) {
checkValidRange(max, this.#_score_range, this.#_invalid_range_code, this.#_error_class))) {
this.#max = max;
}
}
@@ -263,15 +266,18 @@ export class CMIArray extends BaseCMI {
* Constructor cmi *.n arrays
* @param {string} children
* @param {number} errorCode
* @param {class} errorClass
*/
constructor({children, errorCode}) {
constructor({children, errorCode, errorClass}) {
super();
this.#_children = children;
this.#errorCode = errorCode;
this.#errorClass = errorClass;
this.childArray = [];
}
#errorCode;
#errorClass;
#_children;
/**
@@ -287,7 +293,7 @@ export class CMIArray extends BaseCMI {
* @param {string} _children
*/
set _children(_children) {
throw new ValidationError(this.#errorCode);
throw new this.#errorClass.prototype.constructor(this.#errorCode);
}
/**
@@ -303,7 +309,7 @@ export class CMIArray extends BaseCMI {
* @param {number} _count
*/
set _count(_count) {
throw new ValidationError(this.#errorCode);
throw new this.#errorClass.prototype.constructor(this.#errorCode);
}
/**

View File

@@ -9,7 +9,7 @@ import {
import APIConstants from '../constants/api_constants';
import ErrorCodes from '../constants/error_codes';
import Regex from '../constants/regex';
import {ValidationError} from '../exceptions';
import {Scorm12ValidationError} from '../exceptions';
import * as Utilities from '../utilities';
import * as Util from '../utilities';
@@ -21,21 +21,21 @@ 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 Scorm12ValidationError(scorm12_error_codes.READ_ONLY_ELEMENT);
}
/**
* Helper method for throwing Write Only error
*/
export function throwWriteOnlyError() {
throw new ValidationError(scorm12_error_codes.WRITE_ONLY_ELEMENT);
throw new Scorm12ValidationError(scorm12_error_codes.WRITE_ONLY_ELEMENT);
}
/**
* Helper method for throwing Invalid Set error
*/
function throwInvalidValueError() {
throw new ValidationError(scorm12_error_codes.INVALID_SET_VALUE);
throw new Scorm12ValidationError(scorm12_error_codes.INVALID_SET_VALUE);
}
/**
@@ -49,8 +49,13 @@ export function check12ValidFormat(
value: String,
regexPattern: String,
allowEmptyString?: boolean) {
return checkValidFormat(value, regexPattern,
scorm12_error_codes.TYPE_MISMATCH, allowEmptyString);
return checkValidFormat(
value,
regexPattern,
scorm12_error_codes.TYPE_MISMATCH,
Scorm12ValidationError,
allowEmptyString
);
}
/**
@@ -64,8 +69,13 @@ export function check12ValidRange(
value: any,
rangePattern: String,
allowEmptyString?: boolean) {
return checkValidRange(value, rangePattern,
scorm12_error_codes.VALUE_OUT_OF_RANGE, allowEmptyString);
return checkValidRange(
value,
rangePattern,
scorm12_error_codes.VALUE_OUT_OF_RANGE,
Scorm12ValidationError,
allowEmptyString
);
}
/**
@@ -277,6 +287,7 @@ class CMICore extends BaseCMI {
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
errorClass: Scorm12ValidationError,
});
}
@@ -583,6 +594,7 @@ class CMIObjectives extends CMIArray {
super({
children: scorm12_constants.objectives_children,
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorClass: Scorm12ValidationError,
});
}
}
@@ -858,6 +870,7 @@ class CMIInteractions extends CMIArray {
super({
children: scorm12_constants.interactions_children,
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorClass: Scorm12ValidationError,
});
}
}
@@ -875,10 +888,12 @@ export class CMIInteractionsObject extends BaseCMI {
this.objectives = new CMIArray({
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorClass: Scorm12ValidationError,
children: scorm12_constants.objectives_children,
});
this.correct_responses = new CMIArray({
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
errorClass: Scorm12ValidationError,
children: scorm12_constants.correct_responses_children,
});
}
@@ -1082,6 +1097,7 @@ export class CMIObjectivesObject extends BaseCMI {
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
errorClass: Scorm12ValidationError,
});
}

View File

@@ -10,7 +10,7 @@ import APIConstants from '../constants/api_constants';
import Regex from '../constants/regex';
import ErrorCodes from '../constants/error_codes';
import Responses from '../constants/response_constants';
import {ValidationError} from '../exceptions';
import {Scorm2004ValidationError} from '../exceptions';
import * as Util from '../utilities';
const scorm2004_constants = APIConstants.scorm2004;
@@ -23,21 +23,35 @@ 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 Scorm2004ValidationError(scorm2004_error_codes.READ_ONLY_ELEMENT);
}
/**
* Helper method for throwing Write Only error
*/
function throwWriteOnlyError() {
throw new ValidationError(scorm2004_error_codes.WRITE_ONLY_ELEMENT);
throw new Scorm2004ValidationError(scorm2004_error_codes.WRITE_ONLY_ELEMENT);
}
/**
* Helper method for throwing Type Mismatch error
*/
function throwTypeMismatchError() {
throw new ValidationError(scorm2004_error_codes.TYPE_MISMATCH);
throw new Scorm2004ValidationError(scorm2004_error_codes.TYPE_MISMATCH);
}
/**
* Helper method for throwing Dependency Not Established error
*/
function throwDependencyNotEstablishedError() {
throw new Scorm2004ValidationError(scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
}
/**
* Helper method for throwing Dependency Not Established error
*/
function throwGeneralSetError() {
throw new Scorm2004ValidationError(scorm2004_error_codes.GENERAL_SET_FAILURE);
}
/**
@@ -51,8 +65,13 @@ function check2004ValidFormat(
value: String,
regexPattern: String,
allowEmptyString?: boolean) {
return checkValidFormat(value, regexPattern,
scorm2004_error_codes.TYPE_MISMATCH, allowEmptyString);
return checkValidFormat(
value,
regexPattern,
scorm2004_error_codes.TYPE_MISMATCH,
Scorm2004ValidationError,
allowEmptyString,
);
}
/**
@@ -62,8 +81,12 @@ function check2004ValidFormat(
* @return {boolean}
*/
function check2004ValidRange(value: any, rangePattern: String) {
return checkValidRange(value, rangePattern,
scorm2004_error_codes.VALUE_OUT_OF_RANGE);
return checkValidRange(
value,
rangePattern,
scorm2004_error_codes.VALUE_OUT_OF_RANGE,
Scorm2004ValidationError,
);
}
/**
@@ -189,8 +212,8 @@ export class CMI extends BaseCMI {
*/
set completion_threshold(completion_threshold) {
!this.initialized ?
this.#completion_threshold = completion_threshold :
throwReadOnlyError();
this.#completion_threshold = completion_threshold :
throwReadOnlyError();
}
/**
@@ -289,8 +312,8 @@ export class CMI extends BaseCMI {
*/
set learner_name(learner_name) {
!this.initialized ?
this.#learner_name = learner_name :
throwReadOnlyError();
this.#learner_name = learner_name :
throwReadOnlyError();
}
/**
@@ -325,8 +348,8 @@ export class CMI extends BaseCMI {
*/
set max_time_allowed(max_time_allowed) {
!this.initialized ?
this.#max_time_allowed = max_time_allowed :
throwReadOnlyError();
this.#max_time_allowed = max_time_allowed :
throwReadOnlyError();
}
/**
@@ -359,7 +382,7 @@ export class CMI extends BaseCMI {
*/
set progress_measure(progress_measure) {
if (check2004ValidFormat(progress_measure, scorm2004_regex.CMIDecimal) &&
check2004ValidRange(progress_measure, scorm2004_regex.progress_range)) {
check2004ValidRange(progress_measure, scorm2004_regex.progress_range)) {
this.#progress_measure = progress_measure;
}
}
@@ -378,8 +401,8 @@ export class CMI extends BaseCMI {
*/
set scaled_passing_score(scaled_passing_score) {
!this.initialized ?
this.#scaled_passing_score = scaled_passing_score :
throwReadOnlyError();
this.#scaled_passing_score = scaled_passing_score :
throwReadOnlyError();
}
/**
@@ -451,8 +474,8 @@ export class CMI extends BaseCMI {
*/
set time_limit_action(time_limit_action) {
!this.initialized ?
this.#time_limit_action = time_limit_action :
throwReadOnlyError();
this.#time_limit_action = time_limit_action :
throwReadOnlyError();
}
/**
@@ -604,7 +627,7 @@ class CMILearnerPreference extends BaseCMI {
*/
set audio_level(audio_level) {
if (check2004ValidFormat(audio_level, scorm2004_regex.CMIDecimal) &&
check2004ValidRange(audio_level, scorm2004_regex.audio_range)) {
check2004ValidRange(audio_level, scorm2004_regex.audio_range)) {
this.#audio_level = audio_level;
}
}
@@ -641,7 +664,7 @@ class CMILearnerPreference extends BaseCMI {
*/
set delivery_speed(delivery_speed) {
if (check2004ValidFormat(delivery_speed, scorm2004_regex.CMIDecimal) &&
check2004ValidRange(delivery_speed, scorm2004_regex.speed_range)) {
check2004ValidRange(delivery_speed, scorm2004_regex.speed_range)) {
this.#delivery_speed = delivery_speed;
}
}
@@ -660,7 +683,7 @@ class CMILearnerPreference extends BaseCMI {
*/
set audio_captioning(audio_captioning) {
if (check2004ValidFormat(audio_captioning, scorm2004_regex.CMISInteger) &&
check2004ValidRange(audio_captioning, scorm2004_regex.text_range)) {
check2004ValidRange(audio_captioning, scorm2004_regex.text_range)) {
this.#audio_captioning = audio_captioning;
}
}
@@ -701,6 +724,7 @@ class CMIInteractions extends CMIArray {
super({
children: scorm2004_constants.interactions_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorClass: Scorm2004ValidationError,
});
}
}
@@ -716,6 +740,7 @@ class CMIObjectives extends CMIArray {
super({
children: scorm2004_constants.objectives_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorClass: Scorm2004ValidationError,
});
}
}
@@ -731,6 +756,7 @@ class CMICommentsFromLMS extends CMIArray {
super({
children: scorm2004_constants.comments_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorClass: Scorm2004ValidationError,
});
}
}
@@ -746,6 +772,7 @@ class CMICommentsFromLearner extends CMIArray {
super({
children: scorm2004_constants.comments_children,
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorClass: Scorm2004ValidationError,
});
}
}
@@ -771,10 +798,12 @@ export class CMIInteractionsObject extends BaseCMI {
this.objectives = new CMIArray({
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorClass: Scorm2004ValidationError,
children: scorm2004_constants.objectives_children,
});
this.correct_responses = new CMIArray({
errorCode: scorm2004_error_codes.READ_ONLY_ELEMENT,
errorClass: Scorm2004ValidationError,
children: scorm2004_constants.correct_responses_children,
});
}
@@ -820,8 +849,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 +871,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 +893,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 +916,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 +958,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 +1000,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 +1022,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 +1127,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 +1149,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,12 +1171,11 @@ 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,
scorm2004_regex.progress_range)) {
check2004ValidRange(progress_measure,
scorm2004_regex.progress_range)) {
this.#progress_measure = progress_measure;
}
}
@@ -1175,8 +1195,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)) {
@@ -1232,6 +1251,7 @@ class Scorm2004CMIScore extends CMIScore {
invalidTypeCode: scorm2004_error_codes.TYPE_MISMATCH,
invalidRangeCode: scorm2004_error_codes.VALUE_OUT_OF_RANGE,
decimalRegex: scorm2004_regex.CMIDecimal,
errorClass: Scorm2004ValidationError,
});
}
@@ -1249,7 +1269,7 @@ class Scorm2004CMIScore extends CMIScore {
*/
set scaled(scaled) {
if (check2004ValidFormat(scaled, scorm2004_regex.CMIDecimal) &&
check2004ValidRange(scaled, scorm2004_regex.scaled_range)) {
check2004ValidRange(scaled, scorm2004_regex.scaled_range)) {
this.#scaled = scaled;
}
}

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

@@ -1,19 +1,31 @@
// @flow
import APIConstants from './constants/api_constants';
const scorm12_errors = APIConstants.scorm12.error_descriptions;
const aicc_errors = APIConstants.aicc.error_descriptions;
const scorm2004_errors = APIConstants.scorm2004.error_descriptions;
/**
* Data Validation Exception
* Base Validation Exception
*/
export class ValidationError extends Error {
/**
* Constructor to take in an error message and code
* @param {number} errorCode
* @param {string} errorMessage
* @param {string} detailedMessage
*/
constructor(errorCode: number) {
super(errorCode);
constructor(errorCode: number, errorMessage: String, detailedMessage: String) {
super(errorMessage);
this.#errorCode = errorCode;
this.#errorMessage = errorMessage;
this.#detailedMessage = detailedMessage;
}
#errorCode;
#errorMessage;
#detailedMessage;
/**
* Getter for #errorCode
@@ -24,10 +36,69 @@ export class ValidationError extends Error {
}
/**
* Trying to override the default Error message
* Getter for #errorMessage
* @return {string}
*/
get message() {
return this.#errorCode + '';
get errorMessage() {
return this.#errorMessage;
}
/**
* Getter for #detailedMessage
* @return {string}
*/
get detailedMessage() {
return this.#detailedMessage;
}
}
/**
* SCORM 1.2 Validation Error
*/
export class Scorm12ValidationError extends ValidationError {
/**
* Constructor to take in an error code
* @param {number} errorCode
*/
constructor(errorCode: number) {
if ({}.hasOwnProperty.call(scorm12_errors, String(errorCode))) {
super(errorCode, scorm12_errors[String(errorCode)].basicMessage, scorm12_errors[String(errorCode)].detailMessage);
} else {
super(101, scorm12_errors['101'].basicMessage, scorm12_errors['101'].detailMessage);
}
}
}
/**
* AICC Validation Error
*/
export class AICCValidationError extends ValidationError {
/**
* Constructor to take in an error code
* @param {number} errorCode
*/
constructor(errorCode: number) {
if ({}.hasOwnProperty.call(aicc_errors, String(errorCode))) {
super(errorCode, aicc_errors[String(errorCode)].basicMessage, aicc_errors[String(errorCode)].detailMessage);
} else {
super(101, aicc_errors['101'].basicMessage, aicc_errors['101'].detailMessage);
}
}
}
/**
* SCORM 2004 Validation Error
*/
export class Scorm2004ValidationError extends ValidationError {
/**
* Constructor to take in an error code
* @param {number} errorCode
*/
constructor(errorCode: number) {
if ({}.hasOwnProperty.call(scorm2004_errors, String(errorCode))) {
super(errorCode, scorm2004_errors[String(errorCode)].basicMessage, scorm2004_errors[String(errorCode)].detailMessage);
} else {
super(101, scorm2004_errors['101'].basicMessage, scorm2004_errors['101'].detailMessage);
}
}
}