jsf.js_next_gen
Version:
A next generation typescript reimplementation of jsf.js
101 lines • 5.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Assertions = void 0;
/*! Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const mona_dish_1 = require("mona-dish");
const Const_1 = require("../core/Const");
const Lang_1 = require("./Lang");
/**
* a set of internal code assertions
* which raise an error
*
*/
var Assertions;
(function (Assertions) {
function assertRequestIntegrity(options, elem) {
/*assert if the onerror is set and once if it is set it must be of type function*/
assertFunction(options.getIf(Const_1.ON_ERROR).value);
/*assert if the onevent is set and once if it is set it must be of type function*/
assertFunction(options.getIf(Const_1.ON_EVENT).value);
//improve the error messages if an empty elem is passed
//Assertions.assertElementExists(elem);
assert(elem.isPresent(), Lang_1.ExtLang.getMessage("ERR_MUST_BE_PROVIDED1", "{0}: source must be provided or exist", "source element id"), "faces.ajax.request", "ArgNotSet");
}
Assertions.assertRequestIntegrity = assertRequestIntegrity;
function assertUrlExists(node) {
if (node.attr(Const_1.ATTR_URL).isAbsent()) {
throw Assertions.raiseError(new Error(), Lang_1.ExtLang.getMessage("ERR_RED_URL", null, "processRedirect"), "processRedirect");
}
}
Assertions.assertUrlExists = assertUrlExists;
/**
* checks the xml for various issues which can occur
* and prevent a proper processing
*/
function assertValidXMLResponse(responseXML) {
assert(!responseXML.isAbsent(), Const_1.EMPTY_RESPONSE, Const_1.PHASE_PROCESS_RESPONSE);
assert(!responseXML.isXMLParserError(), responseXML.parserErrorText(Const_1.EMPTY_STR), Const_1.PHASE_PROCESS_RESPONSE);
assert(responseXML.querySelectorAll(Const_1.XML_TAG_PARTIAL_RESP).isPresent(), Const_1.ERR_NO_PARTIAL_RESPONSE, Const_1.PHASE_PROCESS_RESPONSE);
}
Assertions.assertValidXMLResponse = assertValidXMLResponse;
/**
* internal helper which raises an error in the
* format we need for further processing
*
* @param error
* @param message the message
* @param caller
* @param title the title of the error (optional)
* @param name the name of the error (optional)
*/
function raiseError(error, message, caller, title, name) {
let finalTitle = title !== null && title !== void 0 ? title : Const_1.MALFORMEDXML;
let finalName = name !== null && name !== void 0 ? name : Const_1.MALFORMEDXML;
let finalMessage = message !== null && message !== void 0 ? message : Const_1.EMPTY_STR;
//TODO clean up the messy makeException, this is a perfect case for encapsulation and sane defaults
return Lang_1.ExtLang.makeException(error, finalTitle, finalName, "Response", caller || ((arguments.caller) ? arguments.caller.toString() : "_raiseError"), finalMessage);
}
Assertions.raiseError = raiseError;
/*
* using the new typescript 3.7 compiler assertion functionality to improve compiler hinting
* we are not fully there yet, but soon
*/
function assert(value, msg = Const_1.EMPTY_STR, caller = Const_1.EMPTY_STR, title = "Assertion Error") {
if (!value) {
throw Assertions.raiseError(new Error(), msg, caller, title);
}
}
Assertions.assert = assert;
function assertType(value, theType, msg = Const_1.EMPTY_STR, caller = Const_1.EMPTY_STR, title = "Type Assertion Error") {
if ((!!value) && !mona_dish_1.Lang.assertType(value, theType)) {
throw Assertions.raiseError(new Error(), msg, caller, title);
}
}
Assertions.assertType = assertType;
function assertFunction(value, msg = Const_1.EMPTY_STR, caller = Const_1.EMPTY_STR, title = "Assertion Error") {
assertType(value, "function", msg, caller, title);
}
Assertions.assertFunction = assertFunction;
function assertDelay(value) {
if (!(value >= 0)) { // >= 0 abbreviation which covers all cases of non positive values,
// including NaN and non numeric strings, no type equality is deliberate here,
throw new Error("Invalid delay value: " + value);
}
}
Assertions.assertDelay = assertDelay;
})(Assertions || (exports.Assertions = Assertions = {}));
//# sourceMappingURL=Assertions.js.map