summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/mastodon/MastodonLogger.ts
blob: 228f9a631bc382a382c7ee6473709c6c669b61ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { Inject, Injectable } from '@nestjs/common';
import { FastifyRequest } from 'fastify';
import Logger from '@/logger.js';
import { LoggerService } from '@/core/LoggerService.js';
import { ApiError } from '@/server/api/error.js';
import { EnvService } from '@/core/EnvService.js';
import { getBaseUrl } from '@/server/api/mastodon/MastodonClientService.js';

@Injectable()
export class MastodonLogger {
	public readonly logger: Logger;

	constructor(
		@Inject(EnvService)
		private readonly envService: EnvService,

		loggerService: LoggerService,
	) {
		this.logger = loggerService.getLogger('masto-api');
	}

	public error(request: FastifyRequest, error: MastodonError, status: number): void {
		const path = new URL(request.url, getBaseUrl(request)).pathname;
		if (status >= 400 && status <= 499) { // Client errors
			this.logger.debug(`Error in mastodon endpoint ${request.method} ${path}:`, error);
		} else { // Server errors
			this.logger.error(`Error in mastodon endpoint ${request.method} ${path}:`, error);
		}
	}
}

// TODO move elsewhere
export interface MastodonError {
	error: string;
	error_description?: string;
}

export function getErrorData(error: unknown): MastodonError {
	// Axios wraps errors from the backend
	error = unpackAxiosError(error);

	if (!error || typeof(error) !== 'object') {
		return {
			error: 'UNKNOWN_ERROR',
			error_description: String(error),
		};
	}

	if (error instanceof ApiError) {
		return convertApiError(error);
	}

	if ('code' in error && typeof (error.code) === 'string') {
		if ('message' in error && typeof (error.message) === 'string') {
			return convertApiError(error as ApiError);
		}
	}

	if (error instanceof Error) {
		return convertGenericError(error);
	}

	return convertUnknownError(error);
}

function unpackAxiosError(error: unknown): unknown {
	if (error && typeof(error) === 'object') {
		if ('response' in error && error.response && typeof (error.response) === 'object') {
			if ('data' in error.response && error.response.data && typeof (error.response.data) === 'object') {
				if ('error' in error.response.data && error.response.data.error && typeof(error.response.data.error) === 'object') {
					return error.response.data.error;
				}

				return error.response.data;
			}

			// No data - this is a fallback to avoid leaking request/response details in the error
			return undefined;
		}
	}

	return error;
}

function convertApiError(apiError: ApiError): MastodonError {
	const mastoError: MastodonError & Partial<ApiError> = {
		error: apiError.code,
		error_description: apiError.message,
		...apiError,
	};

	delete mastoError.code;
	delete mastoError.message;
	delete mastoError.httpStatusCode;

	return mastoError;
}

function convertUnknownError(data: object = {}): MastodonError {
	return Object.assign({}, data, {
		error: 'INTERNAL_ERROR',
		error_description: 'Internal error occurred. Please contact us if the error persists.',
		id: '5d37dbcb-891e-41ca-a3d6-e690c97775ac',
		kind: 'server',
	});
}

function convertGenericError(error: Error): MastodonError {
	const mastoError: MastodonError & Partial<Error> = {
		error: 'INTERNAL_ERROR',
		error_description: String(error),
		...error,
	};

	delete mastoError.name;
	delete mastoError.message;
	delete mastoError.stack;

	return mastoError;
}

export function getErrorStatus(error: unknown): number {
	if (error && typeof(error) === 'object') {
		// Axios wraps errors from the backend
		if ('response' in error && typeof (error.response) === 'object' && error.response) {
			if ('status' in error.response && typeof(error.response.status) === 'number') {
				return error.response.status;
			}
		}

		if ('httpStatusCode' in error && typeof(error.httpStatusCode) === 'number') {
			return error.httpStatusCode;
		}
	}

	return 500;
}