• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb-js / mongodb-mcp-server / 14760563505

30 Apr 2025 05:28PM UTC coverage: 70.593%. First build
14760563505

Pull #176

github

fmenezes
fix: ApiClientError constructor
Pull Request #176: fix: improve api error messages

154 of 307 branches covered (50.16%)

Branch coverage included in aggregate %.

41 of 70 new or added lines in 2 files covered. (58.57%)

643 of 822 relevant lines covered (78.22%)

37.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/common/atlas/apiClientError.ts
1
import { ApiError } from "./openapi.js";
2

3
export class ApiClientError extends Error {
4
    private constructor(
5
        message: string,
6
        public readonly apiError?: ApiError
1✔
7
    ) {
8
        super(message);
1✔
9
        this.name = "ApiClientError";
1✔
10
    }
11

12
    static fromApiError(
13
        response: Response,
14
        apiError: ApiError,
15
        message: string = `error calling Atlas API`
1✔
16
    ): ApiClientError {
17
        const errorMessage = this.buildErrorMessage(apiError);
1✔
18

19
        return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, apiError);
1✔
20
    }
21

22
    static async fromResponse(
23
        response: Response,
24
        message: string = `error calling Atlas API`
×
25
    ): Promise<ApiClientError> {
NEW
26
        const err = await this.extractError(response);
×
27

NEW
28
        const errorMessage = this.buildErrorMessage(err);
×
29

NEW
30
        const body = err && typeof err === "object" ? err : undefined;
×
31

NEW
32
        return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, body);
×
33
    }
34

35
    private static async extractError(response: Response): Promise<ApiError | string | undefined> {
36
        try {
×
NEW
37
            return (await response.json()) as ApiError;
×
38
        } catch {
NEW
39
            try {
×
NEW
40
                return await response.text();
×
41
            } catch {
42
                return undefined;
×
43
            }
44
        }
45
    }
46

47
    private static buildErrorMessage(error?: string | ApiError): string {
48
        let errorMessage: string = "unknown error";
1✔
49

50
        //eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
51
        switch (typeof error) {
1!
52
            case "object":
53
                errorMessage = error.reason || "unknown error";
1!
54
                if (error.detail && error.detail.length > 0) {
1✔
55
                    errorMessage = `${errorMessage}; ${error.detail}`;
1✔
56
                }
57
                break;
1✔
58
            case "string":
NEW
59
                errorMessage = error;
×
NEW
60
                break;
×
61
        }
62

63
        return errorMessage.trim();
1✔
64
    }
65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc