CREATE COMPUTE MODULE WS_LOG_ERROR_SOAP_FAULT
	CREATE FUNCTION Main() RETURNS BOOLEAN
	BEGIN
		CALL CopyMessageHeaders();
		-- CALL CopyEntireMessage();

		SET OutputRoot.XMLNSC.(XMLNSC.XmlDeclaration)XmlDeclaration.(XMLNSC.Attribute)Version = '1.0';
		SET OutputRoot.XMLNSC.(XMLNSC.XmlDeclaration)XmlDeclaration.(XMLNSC.Attribute)Encoding = 'UTF-8';

		DECLARE soapenv NAMESPACE 'http://schemas.xmlsoap.org/soap/envelope/';

		SET OutputRoot.XMLNSC.soapenv:Fault.faultcode = '9999';
		SET OutputRoot.XMLNSC.soapenv:Fault.faultstring = 'Application generated SOAP Fault';
		SET OutputRoot.XMLNSC.soapenv:Fault.detail.Message = 'Application fault detail message';

		DECLARE i INTEGER 1;
		label1 : BEGIN
			FOR error AS InputRoot.XMLNSC.*[] DO
				IF FIELDTYPE(error.error) IS NOT NULL THEN
					IF (LENGTH(error.error) > 100) THEN
						SET OutputRoot.XMLNSC.soapenv:Fault.faultstring = SUBSTRING(error.error FROM 0 FOR 100);
					ELSE
						SET OutputRoot.XMLNSC.soapenv:Fault.faultstring = error.error;
					END IF;

					SET OutputRoot.XMLNSC.soapenv:Fault.detail.Message = error.errorDetail;

					IF FIELDTYPE(error.errorCode) IS NOT NULL THEN
						SET OutputRoot.XMLNSC.soapenv:Fault.faultcode = error.errorCode;
					END IF;
						
					LEAVE label1;
				END IF;

				SET i = i + 1;
			END FOR;
		END;

		RETURN TRUE;
	END;

	CREATE PROCEDURE CopyMessageHeaders() BEGIN
		DECLARE I INTEGER 1;
		DECLARE J INTEGER;
		SET J = CARDINALITY(InputRoot.*[]);
		WHILE I < J DO
			SET OutputRoot.*[I] = InputRoot.*[I];
			SET I = I + 1;
		END WHILE;
	END;

	CREATE PROCEDURE CopyEntireMessage() BEGIN
		SET OutputRoot = InputRoot;
	END;
END MODULE;