-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add logging to SaaS company gateway and related use cases for better error tracking #1876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
106 changes: 106 additions & 0 deletions
106
backend/test/ava-tests/unit-tests/saas-company-gateway.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { Logger, type LoggerService } from '@nestjs/common'; | ||
| import test from 'ava'; | ||
|
|
||
| // The gateway reads IS_SAAS / MICROSERVICE_JWT_SECRET / SAAS_URL through | ||
| // appConfig — set them before the module (and its appConfig import) loads. | ||
| process.env.IS_SAAS = '1'; | ||
| process.env.MICROSERVICE_JWT_SECRET = 'unit-test-secret'; | ||
| process.env.SAAS_URL = 'http://saas.unit.test'; | ||
|
|
||
| const { SaasCompanyGatewayService } = await import( | ||
| '../../../src/microservices/gateways/saas-gateway.ts/saas-company-gateway.service.js' | ||
| ); | ||
| const { normalizeSaasPath } = await import( | ||
| '../../../src/microservices/gateways/saas-gateway.ts/base-saas-gateway.service.js' | ||
| ); | ||
|
|
||
| const COMPANY_ID = 'b3363e0b-0101-4bc8-86cd-02516d407b62'; | ||
|
|
||
| // Every reason a company lookup comes back empty ends up as a user-facing | ||
| // "Company not found" in the callers, so the gateway must leave the real reason | ||
| // (upstream status + message) in the log. These tests pin those log lines. | ||
| const captured: { level: string; message: string }[] = []; | ||
| const capturingLogger: LoggerService = { | ||
| log: (message: unknown) => captured.push({ level: 'log', message: String(message) }), | ||
| warn: (message: unknown) => captured.push({ level: 'warn', message: String(message) }), | ||
| error: (message: unknown) => captured.push({ level: 'error', message: String(message) }), | ||
| debug: () => {}, | ||
| verbose: () => {}, | ||
| }; | ||
| Logger.overrideLogger(capturingLogger); | ||
|
|
||
| const realFetch = globalThis.fetch; | ||
|
|
||
| function stubFetch(impl: (url: string) => Promise<Response>): void { | ||
| globalThis.fetch = impl as unknown as typeof fetch; | ||
| } | ||
|
|
||
| test.afterEach.always(() => { | ||
| globalThis.fetch = realFetch; | ||
| captured.length = 0; | ||
| }); | ||
|
|
||
| // global fetch + shared log capture are mutated per test -> serial only | ||
| test.serial('2xx with company data -> returns the body, nothing logged', async (t) => { | ||
| const seenUrls: string[] = []; | ||
| stubFetch(async (url) => { | ||
| seenUrls.push(url); | ||
| return new Response( | ||
| JSON.stringify({ id: COMPANY_ID, createdAt: '2026-09-08T11:00:00.000Z', updatedAt: '2026-09-08T11:00:00.000Z' }), | ||
| { status: 200 }, | ||
| ); | ||
| }); | ||
| const gateway = new SaasCompanyGatewayService(); | ||
| const info = await gateway.getCompanyInfo(COMPANY_ID); | ||
| t.is(info?.id, COMPANY_ID); | ||
| t.deepEqual(seenUrls, [`http://saas.unit.test/webhook/company/${COMPANY_ID}/`]); | ||
| t.deepEqual(captured, []); | ||
| }); | ||
|
|
||
| test.serial('401 from the saas (secret mismatch) -> null, and the status + saas message are logged', async (t) => { | ||
| stubFetch(async () => new Response(JSON.stringify({ message: 'Invalid token' }), { status: 401 })); | ||
| const gateway = new SaasCompanyGatewayService(); | ||
| const info = await gateway.getCompanyInfo(COMPANY_ID); | ||
| t.is(info, null); | ||
| const warnings = captured.filter((entry) => entry.level === 'warn').map((entry) => entry.message); | ||
| t.true( | ||
| warnings.some((message) => message.includes('HTTP 401') && message.includes('Invalid token')), | ||
| `expected the upstream status and message in the log, got: ${JSON.stringify(warnings)}`, | ||
| ); | ||
| t.true(warnings.some((message) => message.includes(COMPANY_ID) && message.includes('COMPANY_NOT_FOUND'))); | ||
| }); | ||
|
|
||
| test.serial('404 from the saas (row missing there) -> null, logged with the company id and HTTP 404', async (t) => { | ||
| stubFetch( | ||
| async () => | ||
| new Response(JSON.stringify({ message: 'Company not found. Please contact our support team' }), { status: 404 }), | ||
| ); | ||
| const gateway = new SaasCompanyGatewayService(); | ||
| t.is(await gateway.getCompanyInfo(COMPANY_ID), null); | ||
| const warnings = captured.filter((entry) => entry.level === 'warn').map((entry) => entry.message); | ||
| t.true(warnings.some((message) => message.includes(`company ${COMPANY_ID}`) && message.includes('HTTP 404'))); | ||
| }); | ||
|
|
||
| test.serial('2xx without the expected fields -> null, logged as an unexpected body', async (t) => { | ||
| stubFetch(async () => new Response(JSON.stringify({ success: true }), { status: 200 })); | ||
| const gateway = new SaasCompanyGatewayService(); | ||
| t.is(await gateway.getCompanyInfo(COMPANY_ID), null); | ||
| const warnings = captured.filter((entry) => entry.level === 'warn').map((entry) => entry.message); | ||
| t.true(warnings.some((message) => message.includes('unexpected body') && message.includes('success'))); | ||
| }); | ||
|
|
||
| test.serial('fetch throwing (SAAS_URL unreachable) -> rethrows, and the target base URL is logged', async (t) => { | ||
| stubFetch(async () => { | ||
| throw new TypeError('fetch failed'); | ||
| }); | ||
| const gateway = new SaasCompanyGatewayService(); | ||
| await t.throwsAsync(gateway.getCompanyInfo(COMPANY_ID), { message: 'fetch failed' }); | ||
| const errors = captured.filter((entry) => entry.level === 'error').map((entry) => entry.message); | ||
| t.true(errors.some((message) => message.includes('http://saas.unit.test') && message.includes('fetch failed'))); | ||
|
Artuomka marked this conversation as resolved.
Dismissed
|
||
| }); | ||
|
|
||
| test('normalizeSaasPath replaces ids and drops the query so Sentry groups by route shape', (t) => { | ||
| t.is(normalizeSaasPath(`/webhook/company/${COMPANY_ID}/`), '/webhook/company/:id/'); | ||
| t.is(normalizeSaasPath(`/webhook/company/${COMPANY_ID}/domain/?x=1`), '/webhook/company/:id/domain/'); | ||
| t.is(normalizeSaasPath('/webhook/company/domain/app.example.com/'), '/webhook/company/domain/app.example.com/'); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
Normalize custom-domain path segments before fingerprinting.
normalizeSaasPathpreserves the domain in/webhook/company/domain/<domain>/.getCompanyIdByCustomDomainuses that route. A SaaS outage can then create one Sentry issue per tenant domain instead of one grouped failure.Proposed fix
🤖 Prompt for AI Agents