Skip to content

Commit

Permalink
Merge pull request #1174 from serlo/1143-api-sentry-logs-sometimes-do…
Browse files Browse the repository at this point in the history
…nt-help-at-debugging

feat: Add type and payload to sentry logging
  • Loading branch information
kulla authored Dec 4, 2023
2 parents 43b12f3 + 274e088 commit e5fa3b1
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions __tests__/examples/data-sources/how-to-create-a-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ describe('How to create a mutation in a data source: update the content of an ar
// the right values.
return (await res.json()) as unknown
},

type: 'ExampleMutation',
}),
},
}
Expand Down
3 changes: 3 additions & 0 deletions __tests__/examples/data-sources/how-to-create-a-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ describe('How to create a query in a data source: Fetching the content of an art

// Add an example payload which is used in tests
examplePayload: { id: 1 },

// Add a name for sentry logs
type: 'ExampleQuery',
},
// In the actual code you will pass the `environment` variable here
createTestEnvironment(),
Expand Down
2 changes: 2 additions & 0 deletions __tests__/internals/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ test('invalid values from data sources are reported', async () => {
errorContext: {
invalidCurrentValue: invalidValue,
key: 'de.serlo.org/api/uuid/42',
type: 'UuidQuery',
payload: { id: 42 },
},
})
})
2 changes: 2 additions & 0 deletions packages/server/src/internals/data-source-helper/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export class InvalidCurrentValueError extends Error {
invalidCurrentValue: unknown
decoder: string
validationErrors: string[]
type?: string
payload: unknown
key?: string
},
) {
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/internals/data-source-helper/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function createMutation<P, R>(spec: MutationSpec<P, R>): Mutation<P, R> {
invalidCurrentValue: result,
decoder: spec.decoder.name,
validationErrors: reporter.report(decodedResult),
type: spec.type,
payload,
})
}
}
Expand Down Expand Up @@ -54,6 +56,8 @@ interface MutationSpec<Payload, Result> {
* executed and the result of the mutation matches the decoder.
*/
updateCache?: (payload: Payload, newValue: Result) => AsyncOrSync<void>

type: string
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/internals/data-source-helper/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export function createQuery<P, R>(
invalidCurrentValue: value,
decoder: decoder.name,
validationErrors: reporter.report(decoded),
payload,
type: spec.type,
key,
})
}
Expand Down Expand Up @@ -164,6 +166,8 @@ export interface QuerySpec<Payload, Result> {
getPayload: (key: string) => O.Option<Payload>

examplePayload: Payload

type: string
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/internals/data-source-helper/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface RequestSpec<Payload, Result> {
* Function which does the actual query operation.
*/
getCurrentValue: (payload: Payload) => Promise<unknown>

type: string
}

/**
Expand Down Expand Up @@ -46,6 +48,8 @@ export function createRequest<P, R>(spec: RequestSpec<P, R>): Request<P, R> {
throw new InvalidCurrentValueError({
invalidCurrentValue: result,
decoder: spec.decoder.name,
payload,
type: spec.type,
validationErrors: reporter.report(decodedResult),
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/internals/server/kratos-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DatabaseLayer } from '~/model'
const basePath = '/kratos'

const createLegacyUser = createRequest({
type: 'UserCreateMutation',
decoder: DatabaseLayer.getDecoderFor('UserCreateMutation'),
async getCurrentValue(payload: DatabaseLayer.Payload<'UserCreateMutation'>) {
return DatabaseLayer.makeRequest('UserCreateMutation', payload)
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/model/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Environment } from '~/internals/environment'
export function createChatModel({ environment }: { environment: Environment }) {
const getUsersInfo = createQuery(
{
type: 'community.serlo.org/get-users-info',
decoder: t.strict({ success: t.boolean }),
enableSwr: true,
staleAfter: { minutes: 30 },
Expand All @@ -33,6 +34,7 @@ export function createChatModel({ environment }: { environment: Environment }) {
)

const deleteUser = createMutation({
type: 'community.serlo.org/delete-user',
decoder: t.union([
t.strict({ success: t.literal(true) }),
t.strict({ success: t.literal(false), errorType: t.string }),
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/model/google-spreadsheet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function createGoogleSpreadsheetApiModel({
}) {
const getValues = createQuery<Arguments, E.Either<ErrorEvent, CellValues>>(
{
type: 'google-spreadsheets-api',
enableSwr: true,
getCurrentValue: async (args) => {
const { spreadsheetId, range } = args
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/model/kratos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function createKratosModel({
}) {
const getUserLanguage = createQuery(
{
type: 'kratos.serlo.org/get-user-language',
decoder: t.union([InstanceDecoder, t.null]),
enableSwr: true,
staleAfter: { days: 30 },
Expand Down Expand Up @@ -49,6 +50,7 @@ export function createKratosModel({

const getLastLogin = createQuery(
{
type: 'kratos.serlo.org/get-last-login',
decoder: t.union([t.string, t.null]),
enableSwr: true,
staleAfter: { days: 1 },
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/model/mailchimp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createMutation } from '~/internals/data-source-helper'

export function createMailchimpModel() {
const deleteEmailPermanently = createMutation({
type: 'mailchimp/delete-user',
decoder: t.union([
t.strict({ success: t.literal(true) }),
t.strict({ success: t.literal(false), mailchimpResponse: t.unknown }),
Expand Down
Loading

0 comments on commit e5fa3b1

Please sign in to comment.