mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
Hydro.error; csrf_token validation
This commit is contained in:
1 parent
b22082beba
commit
e15c05d36b
9 files changed
+134
-267
No files matched your search
+72
-237
@@ -1,254 +1,92 @@
|
||||
class UserFacingError extends Error {
|
||||
constructor(type) {
|
||||
super(type);
|
||||
this.code = 500;
|
||||
// this.stack = '';
|
||||
this.params = [];
|
||||
}
|
||||
}
|
||||
/* eslint-disable func-names */
|
||||
const { isClass } = require('./utils');
|
||||
|
||||
class SystemError extends Error {
|
||||
constructor(type) {
|
||||
super(type);
|
||||
this.code = 500;
|
||||
// this.stack = '';
|
||||
this.params = [];
|
||||
}
|
||||
}
|
||||
|
||||
class BadRequestError extends UserFacingError {
|
||||
constructor(type) {
|
||||
super(type);
|
||||
this.code = 400;
|
||||
}
|
||||
}
|
||||
|
||||
class ForbiddenError extends UserFacingError {
|
||||
constructor(type) {
|
||||
super(type);
|
||||
this.code = 403;
|
||||
}
|
||||
}
|
||||
|
||||
class NotFoundError extends UserFacingError {
|
||||
constructor(type) {
|
||||
super(type || 'NotFoundError');
|
||||
this.code = 404;
|
||||
}
|
||||
}
|
||||
|
||||
class RemoteOnlineJudgeError extends UserFacingError {
|
||||
constructor(message) {
|
||||
super('RemoteOnlineJudgeError');
|
||||
this.params = [message];
|
||||
}
|
||||
}
|
||||
|
||||
class AlreadyVotedError extends BadRequestError {
|
||||
constructor(psid, uid) {
|
||||
super('You\'ve already voted.');
|
||||
this.params = [psid, uid];
|
||||
}
|
||||
}
|
||||
|
||||
class LoginError extends ForbiddenError {
|
||||
constructor(uname) {
|
||||
super('LoginError');
|
||||
this.params = [uname];
|
||||
}
|
||||
}
|
||||
|
||||
class UserAlreadyExistError extends ForbiddenError {
|
||||
constructor(uname) {
|
||||
super('UserAlreadyExistError');
|
||||
this.params = [uname];
|
||||
}
|
||||
}
|
||||
|
||||
class InvalidTokenError extends ForbiddenError {
|
||||
constructor(token) {
|
||||
super('InvalidTokenError');
|
||||
this.params = [token];
|
||||
}
|
||||
}
|
||||
|
||||
class BlacklistedError extends ForbiddenError {
|
||||
constructor(ip) {
|
||||
super('Address {0} is blacklisted.');
|
||||
this.params = [ip];
|
||||
}
|
||||
}
|
||||
|
||||
class UserNotFoundError extends NotFoundError {
|
||||
constructor(user) {
|
||||
super('UserNotFoundError');
|
||||
this.params = [user];
|
||||
}
|
||||
}
|
||||
|
||||
class NoProblemError extends NotFoundError {
|
||||
constructor() {
|
||||
super('NoProblemError');
|
||||
}
|
||||
}
|
||||
|
||||
class VerifyPasswordError extends ForbiddenError {
|
||||
constructor() {
|
||||
super('VerifyPasswordError');
|
||||
}
|
||||
}
|
||||
|
||||
class OpcountExceededError extends ForbiddenError {
|
||||
constructor(op, periodSecs, maxOperations) {
|
||||
super('OpcountExceededError');
|
||||
this.params = [op, periodSecs, maxOperations];
|
||||
}
|
||||
}
|
||||
|
||||
class PermissionError extends ForbiddenError {
|
||||
constructor(perm) {
|
||||
super('PermissionError');
|
||||
this.params = [perm];
|
||||
}
|
||||
}
|
||||
|
||||
class PrivilegeError extends ForbiddenError {
|
||||
constructor(priv) {
|
||||
if (priv === global.Hydro.model.builtin.PRIV.PRIV_USER_PROFILE) {
|
||||
super("You're not logged in.");
|
||||
} else {
|
||||
super("You don't have the required privilege.");
|
||||
const Err = (name, ...classMessageCode) => {
|
||||
let Class;
|
||||
let msg;
|
||||
let code;
|
||||
for (const item of classMessageCode) {
|
||||
if (typeof item === 'number') {
|
||||
code = item;
|
||||
} else if (typeof item === 'string') {
|
||||
msg = function () { return item; };
|
||||
} else if (isClass(item)) {
|
||||
Class = item;
|
||||
} else if (typeof item === 'function') {
|
||||
msg = item;
|
||||
}
|
||||
this.params = [priv];
|
||||
}
|
||||
const HydroError = class extends Class { };
|
||||
HydroError.prototype.name = name;
|
||||
if (msg) HydroError.prototype.msg = msg;
|
||||
if (code) HydroError.prototype.code = code;
|
||||
return HydroError;
|
||||
};
|
||||
|
||||
class HydroError extends Error {
|
||||
constructor(...params) {
|
||||
super();
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
|
||||
class ValidationError extends ForbiddenError {
|
||||
constructor(field0, field1) {
|
||||
super('ValidationError');
|
||||
if (!field1) this.params = [field0];
|
||||
else this.params = [field0, field1];
|
||||
}
|
||||
}
|
||||
const UserFacingError = Err('UserFacingError', HydroError, 'UserFacingError', 400);
|
||||
const SystemError = Err('SystemError', HydroError, 'SystemError', 500);
|
||||
|
||||
class ContestNotAttendedError extends ForbiddenError {
|
||||
constructor(tid) {
|
||||
super('You haven\'t attended this contest yet.');
|
||||
this.params = [tid];
|
||||
}
|
||||
}
|
||||
const BadRequestError = Err('BadRequestError', UserFacingError, 400);
|
||||
const ForbiddenError = Err('ForbiddenError', UserFacingError, 403);
|
||||
const NotFoundError = Err('NotFoundError', UserFacingError, 404);
|
||||
const RemoteOnlineJudgeError = Err('RemoteOnlineJudgeError', UserFacingError, 500);
|
||||
|
||||
class ContestAlreadyAttendedError extends ForbiddenError {
|
||||
constructor(tid, uid) {
|
||||
super('You\'ve already attended this contest.');
|
||||
this.params = [tid, uid];
|
||||
const AlreadyVotedError = Err('AlreadyVotedError', ForbiddenError, "You've already voted.");
|
||||
const LoginError = Err('LoginError', ForbiddenError, 'Invalid password for user {0}.');
|
||||
const UserAlreadyExistError = Err('UserAlreadyExistError', ForbiddenError, 'User {0} already exists.');
|
||||
const InvalidTokenError = Err('InvalidTokenError', ForbiddenError);
|
||||
const BlacklistedError = Err('BlacklistedError', ForbiddenError, 'Address or user {0} is blacklisted.');
|
||||
const VerifyPasswordError = Err('VerifyPasswordError', ForbiddenError, "Passwords don't match.");
|
||||
const OpcountExceededError = Err('OpcountExceededError', ForbiddenError, 'Too frequent operations of {0} (limit: {2} operations in {1} seconds).');
|
||||
const PermissionError = Err('PermissionError', ForbiddenError, "You don't have the required permission ({0}) in this domain.");
|
||||
const PrivilegeError = Err('PrivilegeError', ForbiddenError, function () {
|
||||
if (this.params.includes(global.Hydro.model.builtin.PRIV.PRIV_USER_PROFILE)) {
|
||||
return "You're not logged in.";
|
||||
}
|
||||
}
|
||||
return "You don't have the required privilege.";
|
||||
});
|
||||
const ValidationError = Err('ValidationError', ForbiddenError, function () {
|
||||
if (this.params.length === 1) return 'Field {0} validation failed.';
|
||||
return 'Field {0} or {1} validation failed.';
|
||||
});
|
||||
const ContestNotAttendedError = Err('ContestNotAttendedError', ForbiddenError, "You haven't attended this contest yet.");
|
||||
const ContestAlreadyAttendedError = Err('ContestAlreadyAttendedError', ForbiddenError, "You've already attended this contest.");
|
||||
const ContestNotLiveError = Err('ContestNotLiveError', ForbiddenError, 'This contest is not live.');
|
||||
const ContestScoreboardHiddenError = Err('ContestScoreboardHiddenError', ForbiddenError, 'Contest scoreboard is not visible.');
|
||||
const TrainingAlreadyEnrollError = Err('TrainingAlreadyEnrollError', ForbiddenError, "You've already enrolled this training.");
|
||||
const RoleAlreadyExistError = Err('RoleAlreadyExistError', ForbiddenError, 'This role already exists.');
|
||||
const CsrfTokenError = Err('CsrfTokenError', ForbiddenError);
|
||||
|
||||
class ContestNotLiveError extends ForbiddenError {
|
||||
constructor(tid) {
|
||||
super('This contest is not live.');
|
||||
this.params = [tid];
|
||||
}
|
||||
}
|
||||
const UserNotFoundError = Err('UserNotFoundError', NotFoundError, 'User {0} not found.');
|
||||
const NoProblemError = Err('NoProblemError', NotFoundError, 'No problem.');
|
||||
const RecordNotFoundError = Err('RecordNotFoundError', NotFoundError, 'Record {0} not found.');
|
||||
const ProblemDataNotFoundError = Err('ProblemDataNotFoundError', NotFoundError, 'Data of problem {1} not found.');
|
||||
const MessageNotFoundError = Err('MessageNotFoundError', NotFoundError, 'Message {0} not found.');
|
||||
const DocumentNotFoundError = Err('DocumentNotFoundError', NotFoundError, 'Document {2} not found.');
|
||||
|
||||
class ContestScoreboardHiddenError extends ForbiddenError {
|
||||
constructor(tid) {
|
||||
super('Contest scoreboard is not visible.');
|
||||
this.params = [tid];
|
||||
}
|
||||
}
|
||||
|
||||
class TrainingAlreadyEnrollError extends ForbiddenError {
|
||||
constructor(tid, uid) {
|
||||
super("You've already enrolled this training.");
|
||||
this.params = [tid, uid];
|
||||
}
|
||||
}
|
||||
|
||||
class RoleAlreadyExistError extends ForbiddenError {
|
||||
constructor(role) {
|
||||
super('This role already exists.');
|
||||
this.params = [role];
|
||||
}
|
||||
}
|
||||
|
||||
class ProblemNotFoundError extends NotFoundError {
|
||||
constructor(domainId, pid) {
|
||||
super('ProblemNotFoundError');
|
||||
this.params = [domainId, pid];
|
||||
}
|
||||
}
|
||||
|
||||
class RecordNotFoundError extends NotFoundError {
|
||||
constructor(rid) {
|
||||
super('RecordNotFoundError');
|
||||
this.params = [rid];
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionNotFoundError extends NotFoundError {
|
||||
constructor(psid) {
|
||||
super('SolutionNotFoundError');
|
||||
this.params = [psid];
|
||||
}
|
||||
}
|
||||
|
||||
class TrainingNotFoundError extends NotFoundError {
|
||||
constructor(tid) {
|
||||
super('TrainingNotFoundError');
|
||||
this.params = [tid];
|
||||
}
|
||||
}
|
||||
|
||||
class ContestNotFoundError extends NotFoundError {
|
||||
constructor(cid) {
|
||||
super('ContestNotFoundError');
|
||||
this.params = [cid];
|
||||
}
|
||||
}
|
||||
|
||||
class ProblemDataNotFoundError extends NotFoundError {
|
||||
constructor(pid) {
|
||||
super('Data of problem {0} not found.');
|
||||
this.params = [pid];
|
||||
}
|
||||
}
|
||||
|
||||
class DiscussionNodeNotFoundError extends NotFoundError {
|
||||
constructor(type, docId) {
|
||||
super('Discussion node {0}/{1} not found.');
|
||||
this.params = [type, docId];
|
||||
}
|
||||
}
|
||||
|
||||
class DocumentNotFoundError extends NotFoundError {
|
||||
constructor(docId) {
|
||||
super('Document {0} not found.');
|
||||
this.params = [docId];
|
||||
}
|
||||
}
|
||||
|
||||
class DiscussionNotFoundError extends NotFoundError {
|
||||
constructor(did) {
|
||||
super('Discussion {0} not found.');
|
||||
this.params = [did];
|
||||
}
|
||||
}
|
||||
|
||||
class MessageNotFoundError extends NotFoundError {
|
||||
constructor(mid) {
|
||||
super('Message {0} not found.');
|
||||
this.params = [mid];
|
||||
}
|
||||
}
|
||||
const ProblemNotFoundError = Err('ProblemNotFountError', DocumentNotFoundError, 'Problem {1} not found.');
|
||||
const SolutionNotFoundError = Err('SolutionNotFoundError', DocumentNotFoundError);
|
||||
const TrainingNotFoundError = Err('TrainingNotFoundError', DocumentNotFoundError);
|
||||
const ContestNotFoundError = Err('ContestNotFoundError', DocumentNotFoundError);
|
||||
const DiscussionNotFoundError = Err('DiscussionNotFoundError', DocumentNotFoundError, 'Discussion {0} not found.');
|
||||
const DiscussionNodeNotFoundError = Err('DiscussionNodeNotFoundError', DocumentNotFoundError, 'Discussion node {1} not found.');
|
||||
|
||||
global.Hydro.error = module.exports = {
|
||||
Err,
|
||||
HydroError,
|
||||
BadRequestError,
|
||||
BlacklistedError,
|
||||
ForbiddenError,
|
||||
NotFoundError,
|
||||
LoginError,
|
||||
CsrfTokenError,
|
||||
UserAlreadyExistError,
|
||||
InvalidTokenError,
|
||||
UserNotFoundError,
|
||||
@@ -291,9 +129,6 @@ class FileTypeNotAllowedError(ValidationError):
|
||||
def message(self):
|
||||
return 'This type of files are not allowed to be uploaded.'
|
||||
|
||||
class CsrfTokenError(ForbiddenError):
|
||||
pass
|
||||
|
||||
class InvalidOperationError(ForbiddenError):
|
||||
pass
|
||||
|
||||
|
||||
Reference in new issue
Block a user