-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
16 changed files
with
440 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,87 @@ | ||
import { GetContextEffect } from 'redux-saga/effects' | ||
import { getContext as getContextBase, SagaGenerator } from 'typed-redux-saga' | ||
import { AllEffect, CallEffect, GetContextEffect } from 'redux-saga/effects' | ||
import { | ||
all, | ||
call, | ||
getContext as getContextBase, | ||
SagaGenerator | ||
} from 'typed-redux-saga' | ||
|
||
import { ErrorLevel } from '~/models/ErrorReporting' | ||
import { FeatureFlags } from '~/services' | ||
import { | ||
compareSDKResponse, | ||
SDKMigrationFailedError | ||
} from '~/utils/sdkMigrationUtils' | ||
|
||
import { CommonStoreContext } from './storeContext' | ||
|
||
export const getContext = <Prop extends keyof CommonStoreContext>( | ||
prop: Prop | ||
): SagaGenerator<CommonStoreContext[Prop], GetContextEffect> => | ||
getContextBase(prop) | ||
|
||
/** Helper generator that returns a fully-awaited AudiusSDK instance */ | ||
export function* getSDK() { | ||
const audiusSdk = yield* getContext('audiusSdk') | ||
return yield* call(audiusSdk) | ||
} | ||
|
||
/** This effect is used to shadow a migration without affecting the return value. | ||
* It will run two effects in parallel to fetch the legacy and migrated responses, | ||
* compare the results, log the diff, and then return the legacy value. Errors thrown | ||
* by the effect for the migrated response will be caught to avoid bugs in the migrated | ||
* code from causing errors. | ||
*/ | ||
export function* checkSDKMigration<T extends object>({ | ||
legacy: legacyCall, | ||
migrated: migratedCall, | ||
endpointName | ||
}: { | ||
legacy: SagaGenerator<T, CallEffect<T>> | ||
migrated: SagaGenerator<T, CallEffect<T>> | ||
endpointName: string | ||
}) { | ||
const getFeatureEnabled = yield* getContext('getFeatureEnabled') | ||
const reportToSentry = yield* getContext('reportToSentry') | ||
|
||
if (!getFeatureEnabled(FeatureFlags.SDK_MIGRATION_SHADOWING)) { | ||
return yield* legacyCall | ||
} | ||
|
||
const [legacy, migrated] = yield* all([ | ||
legacyCall, | ||
call(function* settle() { | ||
try { | ||
return yield* migratedCall | ||
} catch (e) { | ||
return e instanceof Error ? e : new Error(`${e}`) | ||
} | ||
}) | ||
]) as SagaGenerator<T[], AllEffect<CallEffect<T>>> | ||
|
||
try { | ||
compareSDKResponse({ legacy, migrated }, endpointName) | ||
} catch (e) { | ||
const error = | ||
e instanceof SDKMigrationFailedError | ||
? e | ||
: new SDKMigrationFailedError({ | ||
endpointName, | ||
innerMessage: `Unknown error: ${e}`, | ||
legacyValue: legacy, | ||
migratedValue: migrated | ||
}) | ||
console.warn('SDK Migration failed', error) | ||
yield* call(reportToSentry, { | ||
error, | ||
level: ErrorLevel.Warning, | ||
additionalInfo: { | ||
diff: JSON.stringify(error.diff, null, 2), | ||
legacyValue: JSON.stringify(error.legacyValue, null, 2), | ||
migratedValue: JSON.stringify(error.migratedValue, null, 2) | ||
}, | ||
tags: { endpointName: error.endpointName } | ||
}) | ||
} | ||
return legacy | ||
} |
Oops, something went wrong.