AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
feat(pieces-common): option to keep request body out of http error logs (#16419)
Odai Ahmad61372753c066
diff --git a/bun.lock b/bun.lockindex 6565458b54e..e6eee0aba9c 100644--- a/bun.lock+++ b/bun.lock@@ -215,7 +215,7 @@},"packages/pieces/common": {"name": "@activepieces/pieces-common",- "version": "0.14.1",+ "version": "0.15.0","dependencies": {"@activepieces/core-utils": "workspace:*","@activepieces/pieces-framework": "workspace:*",@@ -9036,7 +9036,7 @@},"packages/pieces/community/teable": {"name": "@activepieces/piece-teable",- "version": "0.3.1",+ "version": "0.3.2","dependencies": {"@activepieces/core-piece-types": "workspace:*","@activepieces/core-utils": "workspace:*",diff --git a/packages/pieces/common/package.json b/packages/pieces/common/package.jsonindex 6a5be024f8d..191eb1536a8 100644--- a/packages/pieces/common/package.json+++ b/packages/pieces/common/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/pieces-common",- "version": "0.14.1",+ "version": "0.15.0","type": "commonjs","sideEffects": false,"main": "./dist/src/index.js",diff --git a/packages/pieces/common/src/lib/http/core/fetch-http-client.ts b/packages/pieces/common/src/lib/http/core/fetch-http-client.tsindex 12ee349fb12..010ee865c92 100644--- a/packages/pieces/common/src/lib/http/core/fetch-http-client.ts+++ b/packages/pieces/common/src/lib/http/core/fetch-http-client.ts@@ -79,7 +79,7 @@ export class FetchHttpClient extends BaseHttpClient {if (response.status < 200 || response.status >= successCeiling) {// A stream response can't carry an error message usefully; read the error body as text.const errorBody = await parseResponseBody(response, responseType === 'stream' ? 'text' : responseType);- const httpError = new HttpError(request.body, { status: response.status, responseBody: errorBody });+ const httpError = new HttpError(request.redactBodyInErrors === true ? REDACTED_BODY : request.body, { status: response.status, responseBody: errorBody });console.error('[HttpClient#(sanitized error message)] Request failed:', httpError);throw httpError;}@@ -93,6 +93,8 @@ export class FetchHttpClient extends BaseHttpClient {}}+const REDACTED_BODY = '[redacted]';+export function acceptsRequestBody(method: HttpMethod): boolean {return method !== HttpMethod.GET && method !== HttpMethod.HEAD;}diff --git a/packages/pieces/common/src/lib/http/core/http-request.ts b/packages/pieces/common/src/lib/http/core/http-request.tsindex d7b4ed4a886..31905195218 100644--- a/packages/pieces/common/src/lib/http/core/http-request.ts+++ b/packages/pieces/common/src/lib/http/core/http-request.ts@@ -15,4 +15,5 @@ export type HttpRequest<RequestBody extends HttpRequestBody = any> = {retries?: number;responseType?: 'arraybuffer' | 'json' | 'blob' | 'text' | 'stream';followRedirects?: boolean;+ redactBodyInErrors?: boolean;};diff --git a/packages/pieces/common/test/http-error-redaction.test.ts b/packages/pieces/common/test/http-error-redaction.test.tsnew file mode 100644index 00000000000..da5087b7794--- /dev/null+++ b/packages/pieces/common/test/http-error-redaction.test.ts@@ -0,0 +1,53 @@+/// <reference types="vitest/globals" />++import { httpClient, HttpError, HttpMethod } from '../src';++function stubFailingFetch(): void {+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ error: 'bad request' }), { status: 400, headers: { 'content-type': 'application/json' } })));+}++async function failingRequest(redactBodyInErrors?: boolean): Promise<{ thrown: unknown; logged: string }> {+ const logSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined);+ const thrown = await httpClient+ .sendRequest({+ method: HttpMethod.POST,+ url: 'https://api.example.com/v1/users',+ body: { email: '[email protected]', password: 'hunter2' },+ ...(redactBodyInErrors === undefined ? {} : { redactBodyInErrors }),+ })+ .catch((error: unknown) => error);+ const logged = JSON.stringify(logSpy.mock.calls.map((call) => call.map((arg) => (arg instanceof Error ? arg.message : arg))));+ logSpy.mockRestore();+ return { thrown, logged };+}++describe('HttpError request body redaction', () => {+ beforeEach(() => stubFailingFetch());+ afterEach(() => vi.unstubAllGlobals());++ it('keeps the request body in the error and the log by default', async () => {+ const { thrown, logged } = await failingRequest();+ expect(thrown).toBeInstanceOf(HttpError);+ expect(thrown instanceof HttpError ? thrown.message : '').toContain('hunter2');+ expect(logged).toContain('hunter2');+ });++ it('replaces the request body with [redacted] when redactBodyInErrors is true', async () => {+ const { thrown, logged } = await failingRequest(true);+ if (!(thrown instanceof HttpError)) {+ throw new Error('expected an HttpError');+ }+ const error = thrown;+ expect(error.message).not.toContain('hunter2');+ expect(error.message).toContain('[redacted]');+ expect(error.response).toEqual({ status: 400, body: { error: 'bad request' } });+ expect(JSON.stringify(error.errorMessage())).not.toContain('hunter2');+ expect(logged).not.toContain('hunter2');+ });++ it('still returns the response body on success with redaction on', async () => {+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ id: 1 }), { status: 200, headers: { 'content-type': 'application/json' } })));+ const response = await httpClient.sendRequest({ method: HttpMethod.POST, url: 'https://api.example.com/v1/users', body: { password: 'x' }, redactBodyInErrors: true });+ expect(response.body).toEqual({ id: 1 });+ });+});diff --git a/packages/pieces/community/teable/package.json b/packages/pieces/community/teable/package.jsonindex 2f8b296b110..20a40b1fecd 100644--- a/packages/pieces/community/teable/package.json+++ b/packages/pieces/community/teable/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/piece-teable",- "version": "0.3.1",+ "version": "0.3.2","type": "commonjs","main": "./dist/src/index.js","types": "./dist/src/index.d.ts",diff --git a/packages/pieces/community/teable/vitest.config.ts b/packages/pieces/community/teable/vitest.config.tsindex 79c97261904..eaa3cf7b98a 100644--- a/packages/pieces/community/teable/vitest.config.ts+++ b/packages/pieces/community/teable/vitest.config.ts@@ -9,6 +9,7 @@ export default defineConfig({globals: true,environment: 'node',include: ['test/**/*.test.ts'],+ testTimeout: 30_000,},resolve: {alias: {