From fb7767bf902c05e333be7150f80a922f82765cfe Mon Sep 17 00:00:00 2001 From: Andrei Kakhanouski Date: Sun, 29 Dec 2024 19:37:13 +0100 Subject: [PATCH] Improve the ErrorResponse class --- API_Reference.md | 8 ++++++++ Developer_Guide.md | 18 +++++++++--------- .../default/classes/CustomerWebServiceDemo.cls | 8 ++++---- .../default/classes/libak_ErrorResponse.cls | 18 ++++++++++++++---- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/API_Reference.md b/API_Reference.md index 602fa93..a52c50f 100644 --- a/API_Reference.md +++ b/API_Reference.md @@ -469,6 +469,14 @@ Constructs a new `libak_ErrorResponse` with the specified status code, summary, - `summary` (String): A summary message describing the error. - `details` (String): Additional details about the error. +#### `libak_ErrorResponse(Integer statusCode, String summary)` + +Constructs a new `libak_ErrorResponse` with the specified status code, summary. + +- Parameters: + - `statusCode` (Integer): The HTTP status code for the error response. + - `summary` (String): A summary message describing the error. + #### `libak_ErrorResponse(Integer status, String summary, Exception exc)` Constructs a new `libak_ErrorResponse` with the specified status code and summary, based on an exception. diff --git a/Developer_Guide.md b/Developer_Guide.md index 3a40b73..9418b01 100644 --- a/Developer_Guide.md +++ b/Developer_Guide.md @@ -188,7 +188,7 @@ public class CustomersProcessorV1 extends libak_RestProcessor { ]; if (accounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Accounts are not found', ''); + return new libak_ErrorResponse(404, 'Accounts are not found'); } else { return new libak_JsonResponse(accounts); } @@ -213,7 +213,7 @@ public class CustomerProcessorV1 extends libak_RestProcessor { ]; if (accounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Accounts are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } else { return new libak_JsonResponse(accounts.get(0)); } @@ -224,7 +224,7 @@ public class CustomerProcessorV1 extends libak_RestProcessor { List existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; if (existingAccounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Account are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } Account updatedAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class); @@ -238,7 +238,7 @@ public class CustomerProcessorV1 extends libak_RestProcessor { List existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; if (existingAccounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Account are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } delete existingAccounts.get(0); @@ -303,7 +303,7 @@ global with sharing class CustomerWebServiceDemo { ]; if (accounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Accounts are not found', ''); + return new libak_ErrorResponse(404, 'Accounts are not found'); } else { return new libak_JsonResponse(accounts); } @@ -326,7 +326,7 @@ global with sharing class CustomerWebServiceDemo { ]; if (accounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Accounts are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } else { return new libak_JsonResponse(accounts.get(0)); } @@ -337,7 +337,7 @@ global with sharing class CustomerWebServiceDemo { List existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; if (existingAccounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Account are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } Account updatedAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class); @@ -351,11 +351,11 @@ global with sharing class CustomerWebServiceDemo { List existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; if (existingAccounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Account are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } delete existingAccounts.get(0); - return new libak_SuccessResponse('Account deleted successfully'); + return new libak_SuccessResponse('Account is deleted successfully'); } } } diff --git a/demo-app/main/default/classes/CustomerWebServiceDemo.cls b/demo-app/main/default/classes/CustomerWebServiceDemo.cls index 7de60b7..9a0b0f7 100644 --- a/demo-app/main/default/classes/CustomerWebServiceDemo.cls +++ b/demo-app/main/default/classes/CustomerWebServiceDemo.cls @@ -36,7 +36,7 @@ global with sharing class CustomerWebServiceDemo { ]; if (accounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Accounts are not found', ''); + return new libak_ErrorResponse(404, 'Accounts are not found'); } else { return new libak_JsonResponse(accounts); } @@ -59,7 +59,7 @@ global with sharing class CustomerWebServiceDemo { ]; if (accounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Accounts are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } else { return new libak_JsonResponse(accounts.get(0)); } @@ -70,7 +70,7 @@ global with sharing class CustomerWebServiceDemo { List existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; if (existingAccounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Account are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } Account updatedAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class); @@ -84,7 +84,7 @@ global with sharing class CustomerWebServiceDemo { List existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; if (existingAccounts.isEmpty()) { - return new libak_ErrorResponse(404, 'Account are not found', ''); + return new libak_ErrorResponse(404, 'Account is not found'); } delete existingAccounts.get(0); diff --git a/force-app/main/default/classes/libak_ErrorResponse.cls b/force-app/main/default/classes/libak_ErrorResponse.cls index 7b24337..55cfed2 100644 --- a/force-app/main/default/classes/libak_ErrorResponse.cls +++ b/force-app/main/default/classes/libak_ErrorResponse.cls @@ -21,6 +21,16 @@ public class libak_ErrorResponse implements libak_IRestResponse { this.summary = summary; this.details = details; } + + /** + * Constructs a new `libak_ErrorResponse` with the specified status code, summary, and details. + * + * @param statusCode The HTTP status code for the error response. + * @param summary A summary message describing the error. + */ + public libak_ErrorResponse(Integer statusCode, String summary) { + this(statusCode, summary, summary); + } /** * Constructs a new `libak_ErrorResponse` with the specified status code and summary, @@ -30,8 +40,8 @@ public class libak_ErrorResponse implements libak_IRestResponse { * @param summary A summary message describing the error. * @param exc The exception from which to derive error details. */ - public libak_ErrorResponse(Integer status, String summary, Exception exc) { - this(status, summary, exc?.getMessage()); + public libak_ErrorResponse(Integer statusCode, String summary, Exception exc) { + this(statusCode, summary, exc?.getMessage()); } /** @@ -41,8 +51,8 @@ public class libak_ErrorResponse implements libak_IRestResponse { * @param status The HTTP status code for the error response. * @param exc The exception from which to derive error details. */ - public libak_ErrorResponse(Integer status, Exception exc) { - this(status, exc?.getMessage(), exc); + public libak_ErrorResponse(Integer statusCode, Exception exc) { + this(statusCode, exc?.getMessage(), exc?.getMessage()); } /**