[CORL-540] Logging improvements (#2565)

* fix: enhanced errors around story creation

* feat: enhanced child loggers

* feat: logging enhancements
This commit is contained in:
Wyatt Johnson
2019-09-18 13:07:42 -04:00
committed by Kim Gardner
parent 741739bc16
commit 64f102e6d4
39 changed files with 318 additions and 135 deletions
+6 -3
View File
@@ -19,9 +19,12 @@ type IndexCreationFunction<T> = (
export function createIndexFactory<T>(
collection: Collection<T>
): IndexCreationFunction<T> {
const log = logger.child({
collectionName: collection.collectionName,
});
const log = logger.child(
{
collectionName: collection.collectionName,
},
true
);
return async (
indexSpec: IndexSpecification<T>,
+2 -1
View File
@@ -1,3 +1,4 @@
import { OperationTypeNode } from "graphql";
import { Db } from "mongodb";
import {
@@ -9,7 +10,7 @@ const collection = createCollection<Readonly<PersistedQuery>>("queries");
export interface PersistedQuery {
id: string;
operation: string;
operation: OperationTypeNode;
operationName: string;
query: string;
bundle: string;
+34 -21
View File
@@ -3,7 +3,10 @@ import uuid from "uuid";
import { DeepPartial, Omit } from "coral-common/types";
import { dotize } from "coral-common/utils/dotize";
import { DuplicateStoryURLError } from "coral-server/errors";
import {
DuplicateStoryIDError,
DuplicateStoryURLError,
} from "coral-server/errors";
import {
GQLStoryMetadata,
GQLStorySettings,
@@ -120,14 +123,14 @@ export interface UpsertStoryInput {
export async function upsertStory(
mongo: Db,
tenantID: string,
{ id, url }: UpsertStoryInput,
{ id = uuid.v4(), url }: UpsertStoryInput,
now = new Date()
) {
// Create the story, optionally sourcing the id from the input, additionally
// porting in the tenantID.
const update: { $setOnInsert: Story } = {
$setOnInsert: {
id: id ? id : uuid.v4(),
id,
url,
tenantID,
createdAt: now,
@@ -140,25 +143,35 @@ export async function upsertStory(
},
};
// Perform the find and update operation to try and find and or create the
// story.
const result = await collection(mongo).findOneAndUpdate(
{
url,
tenantID,
},
update,
{
// Create the object if it doesn't already exist.
upsert: true,
try {
// Perform the find and update operation to try and find and or create the
// story.
const result = await collection(mongo).findOneAndUpdate(
{
url,
tenantID,
},
update,
{
// Create the object if it doesn't already exist.
upsert: true,
// False to return the updated document instead of the original
// document.
returnOriginal: false,
// False to return the updated document instead of the original
// document.
returnOriginal: false,
}
);
return result.value || null;
} catch (err) {
// Evaluate the error, if it is in regards to violating the unique index,
// then return a duplicate Story error.
if (err instanceof MongoError && err.code === 11000) {
throw new DuplicateStoryIDError(err, id, url);
}
);
return result.value || null;
throw err;
}
}
export interface FindStoryInput {
@@ -256,7 +269,7 @@ export async function createStory(
// Evaluate the error, if it is in regards to violating the unique index,
// then return a duplicate Story error.
if (err instanceof MongoError && err.code === 11000) {
throw new DuplicateStoryURLError(url);
throw new DuplicateStoryURLError(err, url, id);
}
throw err;
@@ -343,7 +356,7 @@ export async function updateStory(
// Evaluate the error, if it is in regards to violating the unique index,
// then return a duplicate Story error.
if (input.url && err instanceof MongoError && err.code === 11000) {
throw new DuplicateStoryURLError(input.url);
throw new DuplicateStoryURLError(err, input.url, id);
}
throw err;
+1 -1
View File
@@ -556,7 +556,7 @@ export async function insertUser(
await collection(mongo).insert(user);
} catch (err) {
// Evaluate the error, if it is in regards to violating the unique index,
// then return a duplicate Story error.
// then return a duplicate User error.
if (err instanceof MongoError && err.code === 11000) {
// Check if duplicate index was about the email.
if (err.errmsg && err.errmsg.includes("tenantID_1_email_1")) {