view errors.js @ 23:5fc4ee90c1bc

A lot of refactorization. First attempt to modularize the server.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 31 Oct 2010 23:58:07 +0100
parents 9ee956af41e3
children 741110cdcfcf
line wrap: on
line source

/*
 *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 *
 *  This file is part of PSĜS, a PubSub server written in JavaScript.
 *
 *  PSĜS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License.
 *
 *  PSĜS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
 */

var errors = exports;

errors.success = true;
errors.feature_not_implemented = {n: 1, type: 'cancel', error: 'feature-not-implemented'};
errors.node_does_not_exist = {n: 2, type: 'cancel', error: 'item-not-found'};
errors.bad_request = {n: 3, type: 'modify', error: 'bad-request'};
errors.nodeid_required = {n: 4, type: 'modify', error: 'bad-request', reason: 'nodeid-required'};
errors.itemid_required = {n: 5, type: 'modify', error: 'bad-request', reason: 'item-required'},
errors.forbidden = {n: 6, type: 'auth', error: 'forbidden'};
errors.subscriptions_retrieval_not_supported = {n: 23, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'retrieve-subscriptions'};
errors.affiliations_retrieval_not_supported = {n: 29, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'retrieve-affiliations'};

errors.sub = {
	subscribe: {
		jids_do_not_match: {n: 34, type: 'modify', error: 'bad-request', reason: 'invalid-jid'},
		presence_subscription_required: {n: 35, type: 'auth', error: 'not-authorized', reason: 'presence-subscription-required'},
		not_in_roster_group: {n: 36, type: 'auth', error: 'not-authorized', reason: 'not-in-roster-group'},
		not_on_whitelist: {n: 37, type: 'cancel', error: 'not-allowed', reason: 'closed-node'},
		payment_required: {n: 38, type: 'auth', error: 'payment-required'},
		anonymous_subscriptions_not_allowed: {n: 39, type: 'cancel', error: 'forbidden'},
		subscription_pending: {n: 40, type: 'auth', error: 'not-authorized', reason: 'pending-subscription'},
		blocked: {n: 41, type: 'auth', error: 'forbidden'},
		too_many_subscriptions: {n: 42, type: 'wait', error: 'policy-violation', reason: 'too-many-subscriptions'},
		not_supported: {n: 43, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'subscribe'},
		node_has_moved: {n: 44, type: 'modify', error: 'gone', text: true},
		node_does_not_exist: {n: 45, type: 'cancel', error: 'item-not-found'},
		configuration_required: {n: 48, type: 'modify', error: 'not-acceptable', reason: 'configuration-required'},
	},
	unsubscribe: {
		no_subscription_id: {n: 53, type: 'modify', error: 'bad-request', reason: 'subid-required'},
		no_such_subscriber: {n: 54, type: 'cancel', error: 'unexpected-request', reason: 'not-subscribed'},
		insufficient_privileges: {n: 55, type: 'auth', error: 'forbidden'},
		node_does_not_exist: {n: 56, type: 'cancel', error: 'item-not-found'},
		bad_subscription_id: {n: 57, type: 'modify', error: 'not-acceptable', reason: 'invalid-subid'},
	},
	configure: {
		insufficient_privileges: {n: 61, type: 'auth', error: 'forbidden'},
		no_such_subscriber: {n: 62, type: 'modify', error: 'unexpected-request', reason: 'not-subscribed'},
		subscriber_jid_required: {n: 63, type: 'modify', error: 'bad-request', reason: 'jid-required'},
		subscription_id_required: {n: 64, type: 'modify', error: 'bad-request', reason: 'subid-required'},
		invalid_subscription_id: {n: 65, type: 'modify', error: 'not-acceptable', reason: 'invalid-subid'},
		subscription_options_not_supported: {n: 66, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'subscription-options'},
		node_does_not_exist: {n: 67, type: 'cancel', error: 'item-not-found'},
		form_processing_failure: {n: 70, type: 'modify', error: 'bad-request', reason: 'invalid-options'},
	},
	default_options: {
		node_configuration_not_supported: {n: 76, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'subscription-options'},
		default_subscription_configuration_retrieval_not_supported: {n: 77, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'retrieve-default-sub'},
	},
	retrieve: {
		subscription_id_required: {n: 87, type: 'modify', error: 'bad-request', reason: 'subid-required'},
		invalid_subscription_id: {n: 88, type: 'modify', error: 'not-acceptable', reason: 'invalid-subid xmlns'},
		entity_not_subscribed: {n: 89, type: 'auth', error: 'not-authorized', reason: 'not-subscribed'},
		persistent_items_not_supported: {n: 90, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'persistent-items'},
		item_retrieval_not_supported: {n: 91, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'retrieve-items'},
		presence_subscription_required: {n: 92, type: 'auth', error: 'not-authorized', reason: 'presence-subscription-required'},
		not_in_roster_group: {n: 93, type: 'auth', error: 'not-authorized', reason: 'not-in-roster-group'},
		not_on_whitelist: {n: 94, type: 'cancel', error: 'not-allowed', reason: 'closed-node'},
		payment_required: {n: 95, type: 'auth', error: 'payment-required'},
		blocked: {n: 96, type: 'auth', error: 'forbidden'},
		node_does_not_exist: {n: 97, type: 'cancel', error: 'item-not-found'},
	},
};

errors.pub = {
	publish: {
		insufficient_privileges: {n: 105, type: 'auth', error: 'forbidden'},
		item_publication_not_supported: {n: 106, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'publish'},
		node_does_not_exist: {n: 107, type: 'cancel', error: 'item-not-found'},
		payload_too_big: {n: 108, type: 'modify', error: 'not-acceptable', reason: 'payload-too-big'},
		bad_payload: {n: 109, type: 'modify', error: 'bad-request', reason: 'invalid-payload'},
	},
	configuration: {
		no_item: {n: 110, type: 'modify', error: 'bad-request', reason: 'item-required'},
		no_payload: {n: 111, type: 'modify', error: 'bad-request', reason: 'payload-required'},
		transient_item: {n: 112, type: 'modify', error: 'bad-request', reason: 'item-forbidden'},
		precondition: {n: 113, type: 'modify', error: 'conflict', reason: 'precondition-not-met'},

	},
	retract: {
		insufficient_privileges: {n: 118, type: 'auth', error: 'forbidden'},
		node_does_not_exist: {n: 119, type: 'cancel', error: 'item-not-found'},
		nodeid_required: {n: 120, type: 'modify', error: 'bad-request', reason: 'nodeid-required'},
		item_or_itemid_required: {n: 121, type: 'modify', error: 'bad-request', reason: 'item-required'},
		persistent_items_not_supported: {n: 122, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'persistent-items'},
		item_deletion_not_supported: {n: 123, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'delete-items'},
	},
};

errors.owner = {
	create: {
		node_creation_not_supported: {n: 125, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'create-nodes'},
		service_requires_registration: {n: 126, type: 'auth', error: 'registration-required'},
		prohibited: {n: 127, type: 'auth', error: 'forbidden'},
		nodeid_already_exists: {n: 128, type: 'cancel', error: 'conflict'},
		instant_nodes_not_supported: {n: 129, type: 'modify', error: 'not-acceptable', reason: 'nodeid-required'},
		specified_access_model_not_supported: {n: 135, type: 'modify', error: 'not-acceptable', reason: 'unsupported-access-model'},
	},
	configure: {
		node_configuration_not_supported: {n: 140, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'config-node'},
		insufficient_privileges: {n: 141, type: 'auth', error: 'forbidden'},
		nodeid_required: {n: 142, type: 'modify', error: 'bad-request', reason: 'nodeid-required'},
		no_configuration_options: {n: 143, type: 'cancel', error: 'not-allowed'},
		node_does_not_exist: {n: 144, type: 'cancel', error: 'item-not-found'},
		form_processing_failure: {n: 148, type: 'modify', error: 'not-acceptable'},
	},
	default_options: {
		node_configuration_not_supported: {n: 153, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'config-node'},
		default_node_configuration_retrieval_not_supported: {n: 154, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'retrieve-default'},
	},
	delete_node: {
		insufficient_privileges: {n: 159, type: 'auth', error: 'forbidden'},
		node_does_not_exist: {n: 160, type: 'cancel', error: 'item-not-found'},
	},
	purge: {
		node_purging_not_supported: {n: 164, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'purge-nodes'},
		insufficient_privileges: {n: 165, type: 'auth', error: 'forbidden'},
		node_does_not_persist_items: {n: 166, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'persistent-items'},
		node_does_not_exist: {n: 167, type: 'cancel', error: 'item-not-found'},
	},
	//manage_subscription_requests: {},
	pending_subscription_requests: {
		ad_hoc_commands_not_supported: {n: 176, type: 'cancel', error: 'service-unavailable'},
		get_pending_not_supported: {n: 177, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'get-pending'},
		insufficient_privileges: {n: 178, type: 'auth', error: 'forbidden'},
		node_does_not_exist: {n: 179, type: 'cancel', error: 'item-not-found'},
	},
	manage_subscriptions: {
		not_supported: {n: 184, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'manage-subscriptions'},
		retrieve_list: {
			entity_is_not_an_owner: {n: 185, type: 'auth', error: 'forbidden'},
			node_does_not_exist: {n: 186, type: 'cancel', error: 'item-not-found'},
		},
		modify: {
			entity_is_not_an_owner: {n: 190, type: 'auth', error: 'forbidden'},
			node_does_not_exist: {n: 191, type: 'cancel', error: 'item-not-found'},
			multiple_simultaneous_modifications: {n: 193, type: 'modify', error: 'not-acceptable'},
		},
	},
	manage_affiliations: {
		not_supported: {n: 197, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'modify-affiliations'},
		retrieve_list: {
			entity_is_not_an_owner: {n: 198, type: 'auth', error: 'forbidden'},
			node_does_not_exist: {n: 199, type: 'cancel', error: 'item-not-found'},
		},
		modify: {
			requested_affiliation_not_supported: {
				publisher: {n: 203, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'publisher-affiliation'},
				'publish-only': {n: 203, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'publish-only-affiliation'},
				member: {n: 203, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'member-affiliation'},
				outcast: {n: 203, type: 'cancel', error: 'feature-not-implemented', reason: 'unsupported', feature: 'outcast-affiliation'},
			},
			entity_is_not_an_owner: {n: 204, type: 'auth', error: 'forbidden'},
			node_does_not_exist: {n: 205, type: 'cancel', error: 'item-not-found'},
			multiple_simultaneous_modifications: {n: 207, type: 'modify', error: 'not-acceptable'},
		},
	},
};

errors.reverse = {};

function reverseError(o) {
	for (var i in o) {
		if (i == 'reverse' || typeof (o[i]) == 'number');
		else if (!o[i].n)
			reverseError(o[i]);
		else {
			var k = {};
			//var k = {name: i};
			for (var j in o[i]) {
				if (j != 'n')
					k[j] = o[i][j];
			}
			errors.reverse[o[i].n] = k;
		}
	}
}

reverseError(errors);

exports.makeError = function(response, errorNumber, payload) {
	response.attr.type = 'error';
	if (payload)
		response.cnode(payload);

	var e = errors.reverse[errorNumber];
	response.c('error', {type: e.type});

	response.c(e.error, {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();

	if (e.reason) {
		if (e.feature)
			response.c(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors', feature: e.feature}).up();
		else
			response.c(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors'}).up();
	}

	return response;
}