Skip to content

Commit

Permalink
documentation NITs
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Feb 25, 2020
1 parent 9c93b40 commit 6c8a015
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) | |
| [SavedObjectsRepository](./kibana-plugin-server.savedobjectsrepository.md) | |
| [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) | A serializer that can be used to manually convert [raw](./kibana-plugin-server.savedobjectsrawdoc.md) or [sanitized](./kibana-plugin-server.savedobjectsanitizeddoc.md) documents to the other kind. |
| [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) | Registry holding information about all the registered [savedObject types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. |
| [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) | Registry holding information about all the registered [saved object types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. |
| [ScopedClusterClient](./kibana-plugin-server.scopedclusterclient.md) | Serves the same purpose as "normal" <code>ClusterClient</code> but exposes additional <code>callAsCurrentUser</code> method that doesn't use credentials of the Kibana internal user (as <code>callAsInternalUser</code> does) to request Elasticsearch API, but rather passes HTTP headers extracted from the current user request to the API.<!-- -->See [ScopedClusterClient](./kibana-plugin-server.scopedclusterclient.md)<!-- -->. |

## Enumerations
Expand Down Expand Up @@ -229,7 +229,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [RouteValidatorFullConfig](./kibana-plugin-server.routevalidatorfullconfig.md) | Route validations config and options merged into one object |
| [SavedObjectAttribute](./kibana-plugin-server.savedobjectattribute.md) | Type definition for a Saved Object attribute value |
| [SavedObjectAttributeSingle](./kibana-plugin-server.savedobjectattributesingle.md) | Don't use this type, it's simply a helper type for [SavedObjectAttribute](./kibana-plugin-server.savedobjectattribute.md) |
| [SavedObjectMigrationFn](./kibana-plugin-server.savedobjectmigrationfn.md) | A migration function defined for a [saved objects type](./kibana-plugin-server.savedobjectstype.md) used to migrate it's to a given version |
| [SavedObjectMigrationFn](./kibana-plugin-server.savedobjectmigrationfn.md) | A migration function for a [saved object type](./kibana-plugin-server.savedobjectstype.md) used to migrate it to a given version |
| [SavedObjectSanitizedDoc](./kibana-plugin-server.savedobjectsanitizeddoc.md) | |
| [SavedObjectsClientContract](./kibana-plugin-server.savedobjectsclientcontract.md) | Saved Objects is Kibana's data persisentence mechanism allowing plugins to use Elasticsearch for storing plugin state.<!-- -->\#\# SavedObjectsClient errors<!-- -->Since the SavedObjectsClient has its hands in everything we are a little paranoid about the way we present errors back to to application code. Ideally, all errors will be either:<!-- -->1. Caused by bad implementation (ie. undefined is not a function) and as such unpredictable 2. An error that has been classified and decorated appropriately by the decorators in [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md)<!-- -->Type 1 errors are inevitable, but since all expected/handle-able errors should be Type 2 the <code>isXYZError()</code> helpers exposed at <code>SavedObjectsErrorHelpers</code> should be used to understand and manage error responses from the <code>SavedObjectsClient</code>.<!-- -->Type 2 errors are decorated versions of the source error, so if the elasticsearch client threw an error it will be decorated based on its type. That means that rather than looking for <code>error.body.error.type</code> or doing substring checks on <code>error.body.error.reason</code>, just use the helpers to understand the meaning of the error:<!-- -->\`\`\`<!-- -->js if (SavedObjectsErrorHelpers.isNotFoundError(error)) { // handle 404 }<!-- -->if (SavedObjectsErrorHelpers.isNotAuthorizedError(error)) { // 401 handling should be automatic, but in case you wanted to know }<!-- -->// always rethrow the error unless you handle it throw error; \`\`\`<!-- -->\#\#\# 404s from missing index<!-- -->From the perspective of application code and APIs the SavedObjectsClient is a black box that persists objects. One of the internal details that users have no control over is that we use an elasticsearch index for persistance and that index might be missing.<!-- -->At the time of writing we are in the process of transitioning away from the operating assumption that the SavedObjects index is always available. Part of this transition is handling errors resulting from an index missing. These used to trigger a 500 error in most cases, and in others cause 404s with different error messages.<!-- -->From my (Spencer) perspective, a 404 from the SavedObjectsApi is a 404; The object the request/call was targeting could not be found. This is why \#14141 takes special care to ensure that 404 errors are generic and don't distinguish between index missing or document missing.<!-- -->\#\#\# 503s from missing index<!-- -->Unlike all other methods, create requests are supposed to succeed even when the Kibana index does not exist because it will be automatically created by elasticsearch. When that is not the case it is because Elasticsearch's <code>action.auto_create_index</code> setting prevents it from being created automatically so we throw a special 503 with the intention of informing the user that their Elasticsearch settings need to be updated.<!-- -->See [SavedObjectsClient](./kibana-plugin-server.savedobjectsclient.md) See [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) |
| [SavedObjectsClientFactory](./kibana-plugin-server.savedobjectsclientfactory.md) | Describes the factory used to create instances of the Saved Objects Client. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## SavedObjectMigrationFn type

A migration function defined for a [saved objects type](./kibana-plugin-server.savedobjectstype.md) used to migrate it's to a given version
A migration function for a [saved object type](./kibana-plugin-server.savedobjectstype.md) used to migrate it to a given version

<b>Signature:</b>

Expand All @@ -17,11 +17,12 @@ export declare type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, co

```typescript
const migrateProperty: SavedObjectMigrationFn = (doc, { log }) => {
try {
if(doc.attributes.someProp === null) {
log.warn('Skipping migration');
} else {
doc.attributes.someProp = migrateProperty(doc.attributes.someProp);
} catch(e) {
log.warn('Error migrating `someProp`');
}

return doc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The type definition is an aggregation of the legacy savedObjects `schema`<!-- --


```ts
// src/plugins/my_plugin/server/saved_objects/types.ts
// src/plugins/my_plugin/server/saved_objects/my_type.ts
import { SavedObjectsType } from 'src/core/server';
import * as migrations from './migrations';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## SavedObjectsServiceStart.getTypeRegistry property

Returns the [registry](./kibana-plugin-server.isavedobjecttyperegistry.md) containing all registered [savedObject types](./kibana-plugin-server.savedobjectstype.md)
Returns the [registry](./kibana-plugin-server.isavedobjecttyperegistry.md) containing all registered [saved object types](./kibana-plugin-server.savedobjectstype.md)

<b>Signature:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export interface SavedObjectsServiceStart
| [createScopedRepository](./kibana-plugin-server.savedobjectsservicestart.createscopedrepository.md) | <code>(req: KibanaRequest, extraTypes?: string[]) =&gt; ISavedObjectsRepository</code> | Creates a [Saved Objects repository](./kibana-plugin-server.isavedobjectsrepository.md) that uses the credentials from the passed in request to authenticate with Elasticsearch. |
| [createSerializer](./kibana-plugin-server.savedobjectsservicestart.createserializer.md) | <code>() =&gt; SavedObjectsSerializer</code> | Creates a [serializer](./kibana-plugin-server.savedobjectsserializer.md) that is aware of all registered types. |
| [getScopedClient](./kibana-plugin-server.savedobjectsservicestart.getscopedclient.md) | <code>(req: KibanaRequest, options?: SavedObjectsClientProviderOptions) =&gt; SavedObjectsClientContract</code> | Creates a [Saved Objects client](./kibana-plugin-server.savedobjectsclientcontract.md) that uses the credentials from the passed in request to authenticate with Elasticsearch. If other plugins have registered Saved Objects client wrappers, these will be applied to extend the functionality of the client.<!-- -->A client that is already scoped to the incoming request is also exposed from the route handler context see [RequestHandlerContext](./kibana-plugin-server.requesthandlercontext.md)<!-- -->. |
| [getTypeRegistry](./kibana-plugin-server.savedobjectsservicestart.gettyperegistry.md) | <code>() =&gt; ISavedObjectTypeRegistry</code> | Returns the [registry](./kibana-plugin-server.isavedobjecttyperegistry.md) containing all registered [savedObject types](./kibana-plugin-server.savedobjectstype.md) |
| [getTypeRegistry](./kibana-plugin-server.savedobjectsservicestart.gettyperegistry.md) | <code>() =&gt; ISavedObjectTypeRegistry</code> | Returns the [registry](./kibana-plugin-server.isavedobjecttyperegistry.md) containing all registered [saved object types](./kibana-plugin-server.savedobjectstype.md) |

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## SavedObjectTypeRegistry class

Registry holding information about all the registered [savedObject types](./kibana-plugin-server.savedobjectstype.md)<!-- -->.
Registry holding information about all the registered [saved object types](./kibana-plugin-server.savedobjectstype.md)<!-- -->.

<b>Signature:</b>

Expand Down
18 changes: 9 additions & 9 deletions src/core/MIGRATION_EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,8 @@ router.get(
## Saved Objects types
In the legacy platform, saved object types were registered using static definitions in `uiExports` part
the plugin manifest
In the legacy platform, saved object types were registered using static definitions in the `uiExports` part of
the plugin manifest.
In the new platform, all these registration are to be performed programmatically during your plugin's `setup` phase,
using the core `savedObjects`'s `registerType` setup API.
Expand All @@ -756,7 +756,7 @@ and `mappings`.
Let say we have the following in a legacy plugin:
```js
// src/legacy/core_plugins/my-plugin/index.js
// src/legacy/core_plugins/my_plugin/index.js
import mappings from './mappings.json';
import { migrations } from './migrations';

Expand All @@ -780,7 +780,7 @@ new kibana.Plugin({
```
```json
// src/legacy/core_plugins/my-plugin/mappings.json
// src/legacy/core_plugins/my_plugin/mappings.json
{
"first-type": {
"properties": {
Expand All @@ -806,7 +806,7 @@ new kibana.Plugin({
```
```js
// src/legacy/core_plugins/my-plugin/migrations.js
// src/legacy/core_plugins/my_plugin/migrations.js
export const migrations = {
'first-type': {
'1.0.0': migrateFirstTypeToV1,
Expand All @@ -823,7 +823,7 @@ To migrate this, we will have to regroup the declaration per-type. That would be
First type:
```typescript
// src/plugins/my-plugin/server/saved_objects/first_type.ts
// src/plugins/my_plugin/server/saved_objects/first_type.ts
import { SavedObjectsType } from 'src/core/server';

export const firstType: SavedObjectsType = {
Expand All @@ -850,7 +850,7 @@ export const firstType: SavedObjectsType = {
Second type:
```typescript
// src/plugins/my-plugin/server/saved_objects/second_type.ts
// src/plugins/my_plugin/server/saved_objects/second_type.ts
import { SavedObjectsType } from 'src/core/server';

export const secondType: SavedObjectsType = {
Expand All @@ -876,7 +876,7 @@ export const secondType: SavedObjectsType = {
Registration in the plugin's setup phase:
```typescript
// src/plugins/my-plugin/server/plugin.ts
// src/plugins/my_plugin/server/plugin.ts
import { firstType, secondType } from './saved_objects';

export class MyPlugin implements Plugin {
Expand All @@ -896,7 +896,7 @@ The NP `registerType` expected input is very close to the legacy format. However
- The `schema.indexPattern` was accepting either a `string` or a `(config: LegacyConfig) => string`. `SavedObjectsType.indexPattern` only accepts a string, as you can access the configuration during your plugin's setup phase.
- The migration function signature has changed:
In legacy, is was `(doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc;`
In legacy, it was `(doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc;`
In new platform, it is now `(doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;`
With context being:
Expand Down
11 changes: 6 additions & 5 deletions src/core/server/saved_objects/migrations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ import { SavedObjectUnsanitizedDoc } from '../serialization';
import { SavedObjectsMigrationLogger } from './core/migration_logger';

/**
* A migration function defined for a {@link SavedObjectsType | saved objects type}
* used to migrate it's {@link SavedObjectUnsanitizedDoc | documents} to a given version
* A migration function for a {@link SavedObjectsType | saved object type}
* used to migrate it to a given version
*
* @example
* ```typescript
* const migrateProperty: SavedObjectMigrationFn = (doc, { log }) => {
* try {
* if(doc.attributes.someProp === null) {
* log.warn('Skipping migration');
* } else {
* doc.attributes.someProp = migrateProperty(doc.attributes.someProp);
* } catch(e) {
* log.warn('Error migrating `someProp`');
* }
*
* return doc;
* }
* ```
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface SavedObjectsServiceSetup {
*
* @example
* ```ts
* // src/plugins/my_plugin/server/saved_objects/types.ts
* // src/plugins/my_plugin/server/saved_objects/my_type.ts
* import { SavedObjectsType } from 'src/core/server';
* import * as migrations from './migrations';
*
Expand Down Expand Up @@ -205,7 +205,7 @@ export interface SavedObjectsServiceStart {
createSerializer: () => SavedObjectsSerializer;
/**
* Returns the {@link ISavedObjectTypeRegistry | registry} containing all registered
* {@link SavedObjectsType | savedObject types}
* {@link SavedObjectsType | saved object types}
*/
getTypeRegistry: () => ISavedObjectTypeRegistry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type ISavedObjectTypeRegistry = Pick<
>;

/**
* Registry holding information about all the registered {@link SavedObjectsType | savedObject types}.
* Registry holding information about all the registered {@link SavedObjectsType | saved object types}.
*
* @public
*/
Expand Down
1 change: 0 additions & 1 deletion src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,6 @@ export interface SavedObjectMigrationContext {
}

// Warning: (ae-forgotten-export) The symbol "SavedObjectUnsanitizedDoc" needs to be exported by the entry point index.d.ts
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "kibana" does not have an export "SavedObjectUnsanitizedDoc"
//
// @public
export type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;
Expand Down

0 comments on commit 6c8a015

Please sign in to comment.