<?php
// All existing errors



// All existing error codes as integers
const E_NOERROR        = 0;    // No error
// Unknown (unspecific) errors
const E_UNS_UNEXPECTED = 101;  // Unexpected result
const E_UNS_NOTFOUND   = 102;  // Object not found
const E_UNS_INTERNAL   = 103;  // Internal error occured
const E_UNS_JSONBADINP = 104;  // Cant encode object to JSON string
const E_UNS_NOTIMPL    = 105;  // Not yet implemented
// User input errors
const E_UIN_WRONGID    = 201;  // Wrong object id (not found)
const E_UIN_WRONGPATH  = 202;  // Wrong object path (not found)
const E_UIN_FILE2LARGE = 203;  // File size is too large
const E_UIN_FILETYPE   = 204;  // Wrong file type
const E_UIN_IMGBADRES  = 205;  // Invalid image resolution
const E_UIN_INSUFARGS  = 206;  // Not enough arguments was supplied to method
const E_UIN_BADARGS    = 207;  // Bad arguments
const E_UIN_FAIL2UPLD  = 208;  // Failed to upload file
// Authentication errors
const E_AUT_ALRLOGIN   = 301;  // User is already logged in
const E_AUT_REGCLOSED  = 302;  // Registrations are closed
const E_AUT_PWD2WEAK   = 303;  // Password is too weak
const E_AUT_NOTAUTHED  = 304;  // Not authenticated
const E_AUT_WRONGCREDS = 305;  // User with that credentials does not exist
// Access errors
const E_ACS_PERMDENIED = 401;  // Permission to object denied
const E_ACS_INSUFROLE  = 402;  // Insufficient role
// Database-related errors
const E_DBE_UNKNOWN    = 500;  // Unknown error
const E_DBE_INSERTFAIL = 501;  // INSERT query failed
const E_DBE_SELECTFAIL = 502;  // SELECT query failed
const E_DBE_DELETEFAIL = 503;  // DELETE query failed



// All existing errors as two-dimensional array
$Errors_Enum = array(
	array("noerror", E_NOERROR, "no error"),
	// Unspecific errors
	array("uns.unexpected", E_UNS_UNEXPECTED, "unexpected result"),
	array("uns.notfound", E_UNS_NOTFOUND, "object not found"),
	array("uns.internal", E_UNS_INTERNAL, "internal error occured"),
	array("uns.jsonbadinp", E_UNS_JSONBADINP, "cant encode object to json string"),
	array("uns.notimpl", E_UNS_NOTIMPL, "not yet implemented"),
	// User input errors
	array("uin.wrongid", E_UIN_WRONGID, "wrong object id (not found)"),
	array("uin.wrongpath", E_UIN_WRONGPATH, "wrong object path (not found)"),
	array("uin.file2large", E_UIN_FILE2LARGE, "file size is too large"),
	array("uin.filetype", E_UIN_FILETYPE, "wrong file type"),
	array("uin.imgbadres", E_UIN_IMGBADRES, "invalid image resolution"),
	array("uin.insufargs", E_UIN_INSUFARGS, "not enough arguments was supplied to method"),
	array("uin.badargs", E_UIN_BADARGS, "bad arguments"),
	array("uin.fail2upld", E_UIN_FAIL2UPLD, "failed to upload file"),
	// Authentication errors
	array("aut.alrlogin", E_AUT_ALRLOGIN, "already logged in"),
	array("aut.regclosed", E_AUT_REGCLOSED, "registrations are closed"),
	array("aut.pwd2weak", E_AUT_PWD2WEAK, "password is too weak"),
	array("aut.notauthed", E_AUT_NOTAUTHED, "not authenticated"),
	array("aut.wrongcreds", E_AUT_WRONGCREDS, "no such user name and/or password"),
	// Access errors
	array("acs.permdenied", E_ACS_PERMDENIED, "permission denied"),
	array("acs.insufrole", E_ACS_INSUFROLE, "insufficient role"),
	// Database-related errors
	array("dbe.unknown", E_DBE_UNKNOWN, "unknown database error"),
	array("dbe.insertfail", E_DBE_INSERTFAIL, "insert query failed"),
	array("dbe.selectfail", E_DBE_SELECTFAIL, "select query failed"),
	array("dbe.deletefail", E_DBE_DELETEFAIL, "delete query failed")
);



// Get error code by its name
function Errors_ResolveCodeByName (string $name): int {
	global $Errors_Enum;
	$m = count($Errors_Enum);
	for ($i = 0; $i < $m; ++$i) {
		if ($Errors_Enum[$i][0] === $name)
			return $Errors_Enum[$i][1];
	}
	return -1;
}

// Get error name by its code
function Errors_ResolveNameByCode (int $id): string {
	global $Errors_Enum;
	$m = count($Errors_Enum);
	for ($i = 0; $i < $m; ++$i) {
		if ($Errors_Enum[$i][1] === $id)
			return $Errors_Enum[$i][0];
	}
	return "";
}

// Get error short description by its code
function Errors_ResolveDescByCode (int $id): string {
	global $Errors_Enum;
	$m = count($Errors_Enum);
	for ($i = 0; $i < $m; ++$i) {
		if ($Errors_Enum[$i][1] === $id)
			return $Errors_Enum[$i][2];
	}
	return "";
}



?>