mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
errors working with babel 6, export v3 shared modules in main common module
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
var pathToCommon;
|
'use strict';
|
||||||
|
|
||||||
|
let pathToCommon;
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
pathToCommon = './transpiled-babel/index';
|
pathToCommon = './transpiled-babel/index';
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
import extendableBuiltin from './libs/extendableBuiltin';
|
||||||
|
|
||||||
// Base class for custom application errors
|
// Base class for custom application errors
|
||||||
// It extends Error and capture the stack trace
|
// It extends Error and capture the stack trace
|
||||||
export class CustomError extends Error {
|
export class CustomError extends extendableBuiltin(Error) {
|
||||||
constructor () {
|
constructor () {
|
||||||
super();
|
super();
|
||||||
Error.captureStackTrace(this, this.constructor);
|
Error.captureStackTrace(this, this.constructor);
|
||||||
|
|||||||
11
common/script/api-v3/libs/extendableBuiltin.js
Normal file
11
common/script/api-v3/libs/extendableBuiltin.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Babel 6 doesn't support extending native class (Error, Array, ...)
|
||||||
|
// This function makes it possible to extend native classes with the same results as Babel 5
|
||||||
|
module.exports = function extendableBuiltin (klass) {
|
||||||
|
function ExtendableBuiltin () {
|
||||||
|
klass.apply(this, arguments);
|
||||||
|
}
|
||||||
|
ExtendableBuiltin.prototype = Object.create(klass.prototype);
|
||||||
|
Object.setPrototypeOf(ExtendableBuiltin, klass);
|
||||||
|
|
||||||
|
return ExtendableBuiltin;
|
||||||
|
};
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import {
|
import {
|
||||||
NotAuthorized,
|
NotAuthorized,
|
||||||
} from '../../../website/src/libs/api-v3/errors';
|
} from './errors';
|
||||||
import i18n from '../i18n';
|
import i18n from '../i18n';
|
||||||
|
|
||||||
const MAX_TASK_VALUE = 21.27;
|
const MAX_TASK_VALUE = 21.27;
|
||||||
|
|||||||
@@ -18,8 +18,17 @@ var $w, preenHistory, sortOrder,
|
|||||||
import content from './content/index';
|
import content from './content/index';
|
||||||
import i18n from './i18n';
|
import i18n from './i18n';
|
||||||
|
|
||||||
|
import * as errors from './api-v3/errors';
|
||||||
|
import scoreTask from './api-v3/scoreTask';
|
||||||
|
|
||||||
let api = module.exports = {};
|
let api = module.exports = {};
|
||||||
|
|
||||||
|
// Temporary location of API v3 files (soon to be removed)
|
||||||
|
api.v3 = {
|
||||||
|
scoreTask,
|
||||||
|
errors,
|
||||||
|
};
|
||||||
|
|
||||||
api.i18n = i18n;
|
api.i18n = i18n;
|
||||||
api.shouldDo = shouldDo;
|
api.shouldDo = shouldDo;
|
||||||
|
|
||||||
|
|||||||
@@ -10,13 +10,14 @@ import {
|
|||||||
NotAuthorized,
|
NotAuthorized,
|
||||||
BadRequest,
|
BadRequest,
|
||||||
} from '../../libs/api-v3/errors';
|
} from '../../libs/api-v3/errors';
|
||||||
import shared from '../../../../common';
|
import common from '../../../../common';
|
||||||
import Q from 'q';
|
import Q from 'q';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import scoreTask from '../../../../common/script/api-v3/scoreTask';
|
|
||||||
import { preenHistory } from '../../libs/api-v3/preening';
|
import { preenHistory } from '../../libs/api-v3/preening';
|
||||||
|
|
||||||
|
const scoreTask = common.v3.scoreTask;
|
||||||
|
|
||||||
let api = {};
|
let api = {};
|
||||||
|
|
||||||
// challenge must be passed only when a challenge task is being created
|
// challenge must be passed only when a challenge task is being created
|
||||||
@@ -334,8 +335,8 @@ api.updateTask = {
|
|||||||
|
|
||||||
function _generateWebhookTaskData (task, direction, delta, stats, user) {
|
function _generateWebhookTaskData (task, direction, delta, stats, user) {
|
||||||
let extendedStats = _.extend(stats, {
|
let extendedStats = _.extend(stats, {
|
||||||
toNextLevel: shared.tnl(user.stats.lvl),
|
toNextLevel: common.tnl(user.stats.lvl),
|
||||||
maxHealth: shared.maxHealth,
|
maxHealth: common.maxHealth,
|
||||||
maxMP: user._statsComputed.maxMP, // TODO refactor as method not getter
|
maxMP: user._statsComputed.maxMP, // TODO refactor as method not getter
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { CustomError } from '../../../../common/script/api-v3/errors';
|
import common from '../../../../common/script';
|
||||||
|
|
||||||
|
export const CustomError = common.v3.errors.CustomError;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine NotAuthorized
|
* @apiDefine NotAuthorized
|
||||||
@@ -11,7 +13,7 @@ import { CustomError } from '../../../../common/script/api-v3/errors';
|
|||||||
* "message": "Not authorized."
|
* "message": "Not authorized."
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
export { NotAuthorized } from '../../../../common/script/api-v3/errors';
|
export const NotAuthorized = common.v3.errors.NotAuthorized;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine BadRequest
|
* @apiDefine BadRequest
|
||||||
@@ -24,7 +26,7 @@ export { NotAuthorized } from '../../../../common/script/api-v3/errors';
|
|||||||
* "message": "Bad request."
|
* "message": "Bad request."
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
export { BadRequest } from '../../../../common/script/api-v3/errors';
|
export const BadRequest = common.v3.errors.BadRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine NotFound
|
* @apiDefine NotFound
|
||||||
@@ -37,7 +39,7 @@ export { BadRequest } from '../../../../common/script/api-v3/errors';
|
|||||||
* "message": "Not found."
|
* "message": "Not found."
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
export { NotFound } from '../../../../common/script/api-v3/errors';
|
export const NotFound = common.v3.errors.NotFound;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine InternalServerError
|
* @apiDefine InternalServerError
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ import Task from '../../models/task';
|
|||||||
import Q from 'q';
|
import Q from 'q';
|
||||||
import Group from '../../models/group';
|
import Group from '../../models/group';
|
||||||
import User from '../../models/user';
|
import User from '../../models/user';
|
||||||
import scoreTask from '../../../../common/script/api-v3/scoreTask';
|
|
||||||
import { preenUserHistory } from '../../libs/api-v3/preening';
|
import { preenUserHistory } from '../../libs/api-v3/preening';
|
||||||
|
|
||||||
|
const scoreTask = common.v3.scoreTask;
|
||||||
|
|
||||||
let clearBuffs = {
|
let clearBuffs = {
|
||||||
str: 0,
|
str: 0,
|
||||||
int: 0,
|
int: 0,
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
// The error handler middleware that handles all errors
|
// The error handler middleware that handles all errors
|
||||||
// and respond to the client
|
// and respond to the client
|
||||||
import logger from '../../libs/api-v3/logger';
|
import logger from '../../libs/api-v3/logger';
|
||||||
import { CustomError } from '../../../../common/script/api-v3/errors';
|
|
||||||
import {
|
import {
|
||||||
|
CustomError,
|
||||||
BadRequest,
|
BadRequest,
|
||||||
InternalServerError,
|
InternalServerError,
|
||||||
} from '../../libs/api-v3/errors';
|
} from '../../libs/api-v3/errors';
|
||||||
|
|||||||
Reference in New Issue
Block a user