Merge remote-tracking branch 'xo-common/master'

This commit is contained in:
Julien Fontanet 2017-01-10 15:20:14 +01:00
commit f31bbcdaab
8 changed files with 390 additions and 0 deletions

View File

@ -0,0 +1,65 @@
# http://EditorConfig.org
#
# Julien Fontanet's configuration
# https://gist.github.com/julien-f/8096213
# Top-most EditorConfig file.
root = true
# Common config.
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespaces = true
# CoffeeScript
#
# https://github.com/polarmobile/coffeescript-style-guide/blob/master/README.md
[*.{,lit}coffee]
indent_size = 2
indent_style = space
# Markdown
[*.{md,mdwn,mdown,markdown}]
indent_size = 4
indent_style = space
# Package.json
#
# This indentation style is the one used by npm.
[/package.json]
indent_size = 2
indent_style = space
# Jade
[*.jade]
indent_size = 2
indent_style = space
# JavaScript
#
# Two spaces seems to be the standard most common style, at least in
# Node.js (http://nodeguide.com/style.html#tabs-vs-spaces).
[*.{js,jsx,ts,tsx}]
indent_size = 2
indent_style = space
# Less
[*.less]
indent_size = 2
indent_style = space
# Sass
#
# Style used for http://libsass.com
[*.s[ac]ss]
indent_size = 2
indent_style = space
# YAML
#
# Only spaces are allowed.
[*.yaml]
indent_size = 2
indent_style = space

7
packages/xo-common/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/dist/
/node_modules/
npm-debug.log
npm-debug.log.*
pnpm-debug.log
pnpm-debug.log.*

View File

@ -0,0 +1,10 @@
/examples/
example.js
example.js.map
*.example.js
*.example.js.map
/test/
/tests/
*.spec.js
*.spec.js.map

View File

@ -0,0 +1,9 @@
language: node_js
node_js:
- stable
- 6
- 4
# Use containers.
# http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false

View File

@ -0,0 +1,49 @@
# xo-common [![Build Status](https://travis-ci.org/vatesfr/xo-common.png?branch=master)](https://travis-ci.org/vatesfr/xo-common)
> Code shared between [XO](https://xen-orchestra.com) server and clients
## Install
Installation of the [npm package](https://npmjs.org/package/xo-common):
```
> npm install --save xo-common
```
## Usage
**TODO**
## Development
```
# Install dependencies
> npm install
# Run the tests
> npm test
# Continuously compile
> npm run dev
# Continuously run the tests
> npm run dev-test
# Build for production (automatically called by npm install)
> npm run build
```
## Contributions
Contributions are *very* welcomed, either on the documentation or on
the code.
You may:
- report any [issue](https://github.com/vatesfr/xo-common/issues)
you've encountered;
- fork and create a pull request.
## License
AGPL3 © [Vates SAS](https://vates.fr)

View File

@ -0,0 +1 @@
module.exports = require('./dist/api-errors')

View File

@ -0,0 +1,83 @@
{
"name": "xo-common",
"version": "0.1.0",
"license": "AGPL-3.0",
"description": "Code shared between [XO](https://xen-orchestra.com) server and clients",
"keywords": [],
"homepage": "https://github.com/vatesfr/xo-common",
"bugs": "https://github.com/vatesfr/xo-common/issues",
"repository": {
"type": "git",
"url": "https://github.com/vatesfr/xo-common.git"
},
"author": {
"name": "Julien Fontanet",
"email": "julien.fontanet@isonoe.net"
},
"preferGlobal": false,
"bin": {},
"files": [
"dist/",
"*.js"
],
"engines": {
"node": ">=4"
},
"dependencies": {
"babel-runtime": "^6.18.0",
"lodash": "^4.16.6",
"make-error": "^1.2.1"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-eslint": "^7.1.0",
"babel-plugin-lodash": "^3.2.9",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-env": "^1.0.0",
"babel-preset-stage-0": "^6.16.0",
"cross-env": "^3.1.3",
"dependency-check": "^2.6.0",
"ghooks": "^1.3.2",
"rimraf": "^2.5.4",
"standard": "^8.5.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/",
"clean": "rimraf dist/",
"depcheck": "dependency-check ./package.json --entry api-errors.js",
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/",
"lint": "standard",
"posttest": "npm run lint && npm run depcheck",
"prebuild": "npm run clean",
"predev": "npm run clean",
"prepublish": "npm run build"
},
"babel": {
"plugins": [
"lodash"
],
"presets": [
[
"env",
{
"targets": {
"browsers": "> 1%",
"node": 4
}
}
],
"stage-0"
]
},
"standard": {
"ignore": [
"dist"
],
"parser": "babel-eslint"
},
"config": {
"ghooks": {
"commit-msg": "npm test"
}
}
}

View File

@ -0,0 +1,166 @@
import { BaseError } from 'make-error'
import { isArray, iteratee } from 'lodash'
class XoError extends BaseError {
constructor ({ code, message, data }) {
super(message)
this.code = code
this.data = data
}
toJsonRpcError () {
return {
message: this.message,
code: this.code,
data: this.data
}
}
}
const create = (code, getProps) => {
const factory = args => new XoError({ ...getProps(args), code })
factory.is = (error, predicate) =>
error.code === code && iteratee(predicate)(error)
return factory
}
// =============================================================================
export const notImplemented = create(0, () => ({
message: 'not implemented'
}))
export const noSuchObject = create(1, (id, type) => ({
data: { id, type },
message: 'no such object'
}))
export const unauthorized = create(2, () => ({
message: 'not authenticated or not enough permissions'
}))
export const invalidCredentials = create(3, () => ({
message: 'invalid credentials'
}))
// Deprecated alreadyAuthenticated (4)
export const forbiddenOperation = create(5, (operation, reason) => ({
data: { operation, reason },
message: `forbidden operation: ${operation}`
}))
// Deprecated GenericError (6)
export const noHostsAvailable = create(7, () => ({
message: 'no hosts available'
}))
export const authenticationFailed = create(8, () => ({
message: 'authentication failed'
}))
export const serverUnreachable = create(9, objectId => ({
data: {
objectId
},
message: 'server unreachable'
}))
export const invalidParameters = create(10, (message, errors) => {
if (isArray(message)) {
errors = message
message = undefined
}
return {
data: { errors },
message: message || 'invalid parameters'
}
})
export const vmMissingPvDrivers = create(11, ({ vm }) => ({
data: {
objectId: vm
},
message: 'missing PV drivers'
}))
export const vmIsTemplate = create(12, ({ vm }) => ({
data: {
objectId: vm
},
message: 'VM is a template'
}))
// TODO: We should probably create a more generic error which gathers all incorrect state errors.
// e.g.:
// incorrectState {
// data: {
// objectId: 'af43e227-3deb-4822-a79b-968825de72eb',
// property: 'power_state',
// actual: 'Running',
// expected: 'Halted'
// },
// message: 'incorrect state'
// }
export const vmBadPowerState = create(13, ({ vm, expected, actual }) => ({
data: {
objectId: vm,
expected,
actual
},
message: `VM state is ${actual} but should be ${expected}`
}))
export const vmLacksFeature = create(14, ({ vm, feature }) => ({
data: {
objectId: vm,
feature
},
message: `VM lacks feature ${feature || ''}`
}))
export const notSupportedDuringUpgrade = create(15, () => ({
message: 'not supported during upgrade'
}))
export const objectAlreadyExists = create(16, ({ objectId, objectType }) => ({
data: {
objectId,
objectType
},
message: `${objectType || 'object'} already exists`
}))
export const vdiInUse = create(17, ({ vdi, operation }) => ({
data: {
objectId: vdi,
operation
},
message: 'VDI in use'
}))
export const hostOffline = create(18, ({ host }) => ({
data: {
objectId: host
},
message: 'host offline'
}))
export const operationBlocked = create(19, ({ objectId, code }) => ({
data: {
objectId,
code
},
message: 'operation blocked'
}))
export const patchPrecheckFailed = create(20, ({ errorType, patch }) => ({
data: {
objectId: patch,
errorType
},
message: `patch precheck failed: ${errorType}`
}))