implemented user service (no auth!)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import 'package:backend_dart/domain/interface/error.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
TaskEither<IError, T> Function(T?) errorOnNull<T>(IError error) {
|
||||
return (T? value) {
|
||||
return value != null ? TaskEither.of(value) : TaskEither.left(error);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import 'dart:convert';
|
||||
import 'package:backend_dart/domain/errors/error_code.dart';
|
||||
import 'package:backend_dart/domain/interface/error.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
|
||||
class ResponseHelpers {
|
||||
/// Sendet eine JSON-Antwort mit einem 200-Statuscode
|
||||
static Response jsonOk(Map<String, dynamic> data) {
|
||||
return Response.ok(
|
||||
jsonEncode(data),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
}
|
||||
|
||||
/// Sendet eine JSON-Antwort mit einem spezifischen Statuscode
|
||||
static Response jsonWithStatus(Map<String, dynamic> data, int statusCode) {
|
||||
return Response(
|
||||
statusCode,
|
||||
body: jsonEncode(data),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
}
|
||||
|
||||
/// Sendet eine JSON-Antwort für einen Fehler (z. B. 404 oder 500)
|
||||
static Response jsonError(String message, {int statusCode = 500}) {
|
||||
return Response(
|
||||
statusCode,
|
||||
body: jsonEncode({'error': message}),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
}
|
||||
|
||||
/// Sendet eine einfache Textantwort
|
||||
static Response textResponse(String message, {int statusCode = 200}) {
|
||||
return Response(
|
||||
statusCode,
|
||||
body: message,
|
||||
headers: {'Content-Type': 'text/plain'},
|
||||
);
|
||||
}
|
||||
|
||||
static Response fromError(IError error) {
|
||||
// Print error to console
|
||||
print('Error: ${error.code.name} - ${error.message}');
|
||||
//print(error.stackTrace);
|
||||
return Response(
|
||||
mapErrorCodeToHttpStatus(error.code),
|
||||
body: jsonEncode({
|
||||
'code': error.code.name,
|
||||
'error': error.message,
|
||||
'details': error.details,
|
||||
}),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
int mapErrorCodeToHttpStatus(ErrorCode errorCode) {
|
||||
switch (errorCode) {
|
||||
case ErrorCode.networkError:
|
||||
return 503; // Service Unavailable
|
||||
case ErrorCode.validationError:
|
||||
case ErrorCode.inputError:
|
||||
return 400; // Bad Request
|
||||
case ErrorCode.unexpectedError:
|
||||
case ErrorCode.internalError:
|
||||
case ErrorCode.databaseError:
|
||||
return 500; // Internal Server Error
|
||||
case ErrorCode.authenticationError:
|
||||
return 401; // Unauthorized
|
||||
case ErrorCode.permissionDenied:
|
||||
return 403; // Forbidden
|
||||
case ErrorCode.notFound:
|
||||
return 404; // Not Found
|
||||
case ErrorCode.mappingError:
|
||||
case ErrorCode.outputError:
|
||||
return 422; // Unprocessable Entity
|
||||
case ErrorCode.exception:
|
||||
case ErrorCode.unknownError:
|
||||
return 520; // Unknown Error (Custom)
|
||||
default:
|
||||
return 500; // Fallback: Internal Server Error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user